Export (0) Print
Expand All

KeyPressEventArgs.KeyChar Property

Gets the character corresponding to the key pressed.

[Visual Basic]
Public ReadOnly Property KeyChar As Char
[C#]
public char KeyChar {get;}
[C++]
public: __property __wchar_t get_KeyChar();
[JScript]
public function get KeyChar() : Char;

Property Value

The ASCII character that is composed. For example, if the user presses the SHIFT + K, this property returns an uppercase K.

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 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

Show:
© 2014 Microsoft