Freigeben über


KeyPressEventArgs.KeyChar-Eigenschaft

Ruft das Zeichen ab, das der gedrückten Taste entspricht, oder legt dieses fest.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
Public Property KeyChar As Char
'Usage
Dim instance As KeyPressEventArgs
Dim value As Char

value = instance.KeyChar

instance.KeyChar = value
public char KeyChar { get; set; }
public:
property wchar_t KeyChar {
    wchar_t get ();
    void set (wchar_t value);
}
/** @property */
public char get_KeyChar ()

/** @property */
public void set_KeyChar (char value)
public function get KeyChar () : char

public function set KeyChar (value : char)

Eigenschaftenwert

Das resultierende ASCII-Zeichen. Wenn der Benutzer beispielsweise UMSCHALT+K drückt, gibt diese Eigenschaft ein großes K zurück.

Hinweise

Mit der KeyChar-Eigenschaft können Sie Tastenanschläge zur Laufzeit abrufen und unter bestimmten Laufzeitbedingungen ändern. Beispielsweise können Sie KeyChar verwenden, um bei der Eingabe von Postleitzahlen nicht numerische Tastenanschläge zu deaktivieren, alle alphabetischen Tastenanschläge in einem Dateneingabefeld in Großbuchstaben umzuwandeln oder die Tastatur bzw. ein anderes Tasteneingabegerät auf bestimmte Tastenkombinationen zu überwachen.

Die folgenden Tasten können abgerufen und festgelegt werden:

  • a-z, A-Z.

  • STRG.

  • Satzzeichen.

  • Numerische Tasten im oberen Bereich der Tastatur und auf der Zehnertastatur.

  • EINGABETASTE.

Die folgenden Tasten können nicht abgerufen oder festgelegt werden:

  • Die TAB-TASTE.

  • EINFG und LÖSCHEN.

  • POS1.

  • ENDE.

  • BILD-AUF und BILD-AB.

  • F1-F2.

  • ALT.

  • Pfeiltasten.

Beispiel

Im folgenden Beispiel wird ein TextBox-Steuerelement erstellt. Die keypressed-Methode verwendet die KeyChar-Eigenschaft, um zu prüfen, ob die EINGABETASTE gedrückt ist. Ist dies der Fall, wird die Handled-Eigenschaft auf true festgelegt, wodurch angegeben ist, dass das Ereignis behandelt wurde.

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

    Private 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(Keys.Return) Then
            e.Handled = True
        End If
    End Sub 'keypressed

    Public Shared Sub Main()
        Application.Run(New Form1())
    End Sub 'Main
End Class 'Form1
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);
    }

    private 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)Keys.Return)
        {
            e.Handled = true;
        }
    }

    public static void Main()
    {
        Application.Run(new Form1());
    }
}
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Windows::Forms;

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

private:
   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( gcnew Form1 );
}
import System.*;
import System.Windows.Forms.*;

public class Form1 extends Form
{
    public Form1()
    {
        // Create a TextBox control.
        TextBox tb = new TextBox();
        this.get_Controls().Add(tb);
        tb.add_KeyPress(new KeyPressEventHandler(KeyPressed));
    } //Form1

    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.get_KeyChar() == (char)(13)) {
            e.set_Handled(true);
        }
    } // KeyPressed

    public static void main(String[] args)
    {
        Application.Run(new Form1());
    } //main
} //Form1

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

KeyPressEventArgs-Klasse
KeyPressEventArgs-Member
System.Windows.Forms-Namespace
Control.KeyPress-Ereignis
IsInputChar