Monday, 16 July 2012

Using KeyDown Event in VB.NET

KeyDown event is a useful event, it helps you to detect whether the key on the keyword is held down. It’s mainly used for validating text in the textboxes.

To test, place a textbox into your form and write the below code inside the KeyDown event if TextBox (take the properties if TextBox and select events of TextBox from the properties, from the event list you have the open the KeyDown event).
TextBox Properties
TextBox KeyDown Event


When you select the KeyDown event of TextBox, it shows like this:
Private Sub TextBox1_KeyDown(ByVal sender As System.Object, _
                                 ByVal e As System.Windows.Forms.KeyEventArgs) _
                                 Handles TextBox1.KeyDown

    End Sub

The event is being handled is the KeyDown event of TextBox.
ByVal e As System.Windows.Forms.KeyEventArgs
Here e holds the information about what key is pressed on the keyboard by the user.
To check which key is pressed on the keyboard is using the below code
If e.KeyCode = Keys.F1 Then
            MsgBox("Help")
        End If

The complete for the KeyEvent in TextBox
Public Class Form1
    Private Sub TextBox1_KeyDown(ByVal sender As System.Object, _
                                 ByVal e As System.Windows.Forms.KeyEventArgs) _
                                 Handles TextBox1.KeyDown

        If e.KeyCode = Keys.F1 Then
            MsgBox("Help")
        End If

    End Sub
End Clas

More Related Topics:
Reactions:

1 comment:

  1. Hi,
    When ever I want, any Dotnet related information; I have reached at your blog directly. You have shared really great information on this.

    ReplyDelete