Home » KeyPress Event in VB.NET

KeyPress Event in VB.NET

by Online Tutorials Library

KeyPress Event in VB.NET

The KeyPress event is used in the Windows Form when a user presses a character, space, or backspace key during the focus on the control, the KeyPress event occurs. Furthermore, the keypress event is raised only when printable keys or numbers such as alphabets (a, b, c) are processed with Windows Form. It is not picked up by non-character keys such as Esc, Shift, Ctrl, etc. to raise the KeyPress event. The Non-character keys are used with KeyDown and KeyUp events in the VB.NET Windows application.

KeyDown event: The KeyDown event occurred when a user presses a key on the keyboard. It repeats while the user keeps the key depressed. The KeyDown event is provided by the Keyboard event in the VB.NET application.

KeyUp event: The KeyUp event is rising when a user releases a key on the keyboard. The KeyUp event is provided by the Keyboard event in the VB.NET application.

Let’s create a simple program to display the KeyPressEvent in the VB.NET Windows Forms.

KeyPressEvent.vb

Output:

KeyPress Event in VB.NET

Press any printable key. We have pressed the key A.

KeyPress Event in VB.NET

After inserting a character, click on the Press button, it shows the following output.

KeyPress Event in VB.NET

Example of KeyDown Event

Let’s create a simple program to display the use of KeyDown events in the VB.NET Windows Forms.

KeyDownEvent1.vb

Output:

KeyPress Event in VB.NET

Press the Enter button, it shows the image shown below.

KeyPress Event in VB.NET

Example of KeyUp Event

Let’s create a simple program to display the use of KeyUp events in the VB.NET Windows Forms.

KeyUp.vb

Output:

KeyPress Event in VB.NET

After releasing the Enter key from the keyboard, it displays the image below.

KeyPress Event in VB.NET


You may also like