Visual Basic - How To Ignore Textbox Keystrokes In VB 6 And VB .NET (VB 6/.NET)

Do you need to ignore a textbox keystroke? If so, you can easily accomplish this task in both VB and VB .NET. In VB, place the following code in the KeyPress() event of the text box:

  • KeyAscii = 0

In VB 6, KeyAscii is a parameter of the KeyPress() event and it contains the Ascii value of the key the user just pressed. Setting the value to 0 in the KeyPress() event nullifies the entry.

In VB .NET, there isn't a KeyAscii event argument. So, to cancel the current keystroke, you'll need to use the Handled property of the e event argument, like so:

  • e.Handled = True

The Handled property tracks which events have been handled and which have not. VB .NET will only pass on events that haven't been handled. Hence, when you set the handled property to True in the KeyPress() event, you're essentially telling .NET that the event has been handled and to ignore it.

Go back