Duration (Pacific Standard Time):
To (Pacific Standard Time):
Impact:
  • None
User Action:
  • None
.NET Framework Class Library

KeyPressEventArgs.Handled Property

Gets or sets a value indicating whether the KeyPress event was handled.

[Visual Basic]
Public Property Handled As Boolean
[C#]
public bool Handled {get; set;}
[C++]
public: __property bool get_Handled();
public: __property void set_Handled(bool);
[JScript]
public function get Handled() : Boolean;
public function set Handled(Boolean);

Property Value

true if the event is handled; otherwise, false.

Remarks

If the event is not handled, it will be sent to the operating system for default processing. Set Handled to true to cancel the KeyPress event.

Example

[Visual Basic, C#, C++] The following example creates a TextBox control. The keypressed method uses the KeyChar property to check whether the ENTER key is pressed. If the ENTER key is pressed, the Handled property is set to true, which indicates the event is handled.

[Visual Basic] 
Imports System
Imports System.Windows.Forms

Public Class Form1
    Inherits Form

    Public Sub New()
        ' Create a TextBox control.
        Dim tb As New TextBox()
        Me.Controls.Add(tb)
        AddHandler tb.KeyPress, AddressOf keypressed
    End Sub 'New

    Sub keypressed(ByVal o As [Object], ByVal e As KeyPressEventArgs)
        ' The keypressed method uses the KeyChar property to check 
        ' whether the ENTER key is pressed. 

        ' If the ENTER key is pressed, the Handled property is set to true, 
        ' to indicate the event is handled.

        If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
            e.Handled = True
        End If
    End Sub 'keypressed

    Public Shared Sub Main()
        Application.Run(New Form1())
    End Sub 'Main
End Class 'Form1

[C#] 

using System;
using System.Windows.Forms;

public class Form1: Form
{
    public Form1()
    {
        // Create a TextBox control.
        TextBox tb = new TextBox();
        this.Controls.Add(tb);
        tb.KeyPress += new KeyPressEventHandler(keypressed);
    }

    void keypressed(Object o, KeyPressEventArgs e)
    {
        // The keypressed method uses the KeyChar property to check 
        // whether the ENTER key is pressed. 

        // If the ENTER key is pressed, the Handled property is set to true, 
        // to indicate the event is handled.
        if(e.KeyChar == (char)13)
            e.Handled=true;
    }

    public static void Main()
    {
        Application.Run(new Form1());
    }
}

[C++] 

#using <mscorlib.dll>
#using <System.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Windows::Forms;

public __gc class Form1: public Form
{
public:
    Form1()
    {
        // Create a TextBox control.
        TextBox* tb = new TextBox();
        this->Controls->Add(tb);
        tb->KeyPress += new KeyPressEventHandler(this,&Form1::keypressed);
    }

    void keypressed(Object* /*o*/, KeyPressEventArgs* e)
    {
        // The keypressed method uses the KeyChar property to check 
        // whether the ENTER key is pressed. 

        // If the ENTER key is pressed, the Handled property is set to true, 
        // to indicate the event is handled.
        if(e->KeyChar == (char)13)
            e->Handled=true;
    }

};

int main()
{
    Application::Run(new Form1());
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

KeyPressEventArgs Class | KeyPressEventArgs Members | System.Windows.Forms Namespace