KeyPressEventArgs.KeyChar Eigenschap
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Hiermee haalt u het teken op dat overeenkomt met de toets ingedrukt of stelt u het teken in.
public:
property char KeyChar { char get(); };
public:
property char KeyChar { char get(); void set(char value); };
public char KeyChar { get; }
public char KeyChar { get; set; }
member this.KeyChar : char
member this.KeyChar : char with get, set
Public ReadOnly Property KeyChar As Char
Public Property KeyChar As Char
Waarde van eigenschap
Het ASCII-teken dat is samengesteld. Als de gebruiker bijvoorbeeld op Shift+K drukt, retourneert deze eigenschap een hoofdletter K.
Voorbeelden
In het volgende voorbeeld wordt een TextBox besturingselement gemaakt. De keypressed methode gebruikt de KeyChar eigenschap om te controleren of de ENTER-toets is ingedrukt. Als de ENTER-toets wordt ingedrukt, wordt de Handled eigenschap ingesteld op true, wat aangeeft dat de gebeurtenis wordt verwerkt.
#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 );
}
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());
}
}
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
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
Public Shared Sub Main()
Application.Run(New Form1())
End Sub
End Class
Opmerkingen
Gebruik de KeyChar eigenschap om toetsaanslagen tijdens runtime te samplen en toetsaanslagen onder speciale runtime-omstandigheden te wijzigen. U kunt KeyChar bijvoorbeeld niet-numerieke toetsdrukken uitschakelen wanneer de gebruiker een postcode invoert, alle alfabetische toetsdrukken wijzigen in hoofdletters in een gegevensinvoerveld of het toetsenbord of een ander sleutelinvoerapparaat controleren voor specifieke toetsencombinaties.
U kunt de volgende sleutels ophalen of instellen:
a-z, A-Z.
CTRL.
Leestekens.
Nummertoetsen, zowel boven aan het toetsenbord als op het numerieke toetsenblok.
VOER.
U kunt de volgende sleutels niet ophalen of instellen:
De TAB-toets.
INSERT en DELETE.
HOME.
EINDE.
PAGE UP en PAGE DOWN.
F1-F2.
ALT.
Pijltoetsen.
Note
Zie de KeyEventArgs klasse voor informatie over het detecteren van een van de hierboven genoemde niet-tekensleutels.