Provides data for the KeyPress event.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Determines whether the specified object is equal to the current object.(Inherited from Object.)
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)
Serves as the default hash function. (Inherited from Object.)
Returns a string that represents the current object.(Inherited from Object.)
A KeyChar property returns an uppercase K.specifies the character that is composed when the user presses a key. For example, when the user presses SHIFT + K, the
A KeyPress event occurs when the user presses a key. Two events that are closely related to the KeyPress event are KeyUp and KeyDown. The KeyDown event precedes each KeyPress event when the user presses a key, and a KeyUp event occurs when the user releases a key. When the user holds down a key, duplicate KeyDown and KeyPress events occur each time the character repeats. One KeyUp event is generated upon release.
With each KeyPress event, a is passed. A KeyEventArgs is passed with each KeyDown and KeyUp event. A KeyEventArgs specifies whether any modifier keys (CTRL, SHIFT, or ALT) were pressed along with another key. (This modifier information can also be obtained through the ModifierKeys property of the Control class.)
Set Handled to true to cancel the KeyPress event. This keeps the control from processing the key press.
Some controls will process certain key strokes on KeyDown. For example, RichTextBox processes the Enter key before KeyPress is called. In such cases, you cannot cancel the KeyPress event, and must cancel the key stroke from KeyDown instead.
For information about the event model, see Handling and Raising Events.
Public Class myKeyPressClass Private Shared keyPressCount As Long = 0 Private Shared backspacePressed As Long = 0 Private Shared returnPressed As Long = 0 Private Shared escPressed As Long = 0 Private textBox1 As TextBox Private Sub myKeyCounter(sender As Object, ex As KeyPressEventArgs) Select Case ex.KeyChar ' Counts the backspaces. Case ControlChars.Back backspacePressed = backspacePressed + 1 ' Counts the ENTER keys. Case ControlChars.Lf returnPressed = returnPressed + 1 ' Counts the ESC keys. Case Convert.ToChar(27) escPressed = escPressed + 1 ' Counts all other keys. Case Else keyPressCount = keyPressCount + 1 End Select textBox1.Text = backspacePressed & " backspaces pressed" & _ ControlChars.Lf & ControlChars.Cr & escPressed & _ " escapes pressed" & ControlChars.CrLf & returnPressed & _ " returns pressed" & ControlChars.CrLf & keyPressCount & _ " other keys pressed" & ControlChars.CrLf ex.Handled = True End Sub 'myKeyCounter End Class 'myKeyPressClass
You must create a new instance of this class. You must also set the event handler. You can do this in the constructor for your class.
Private myKeyPressHandler As New myKeyPressClass() Public Sub New() InitializeComponent() AddHandler textBox1.KeyPress, AddressOf myKeyPressHandler.myKeyCounter End Sub 'NewNew
When the specified event is raised in the control, the attached method is called and the application can execute code in response to the event.
Available since 1.1
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.