Do you want to test for a certain textbox or all textboxes on a form?
If you want to test for the whole form, try this code, otherwise you can just modify for a certain textbox...
Public Sub Form1_KeyPress(ByVal sender as Object, ByVal e as System.Windows.Form.KeyPressEventArgs) Handles MyBase.Keypress
if (Microsoft.VisualBasic.Asc(e.Keychar)) = 121 then
' insert your code
End if
End Sub
Check this link for the ascii chars for all keyboard keys..
A side note controls won't fire a Keypress event for function keys or control keys either. Only the form seems to accept these values. To read more on this check out this article.
To get around this and only check to see if F10 was pressed for one text box try this
Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
If stuff Then
If keyData = keyData.F10 Then
MsgBox("F10 was Pressed"
End If
End If
End Function
Private Sub textbox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCatNo.Enter
stuff = True
End Sub
Private Sub textbox2_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCatNo.Leave
stuff = False
End Sub
stuff is a global private variable of type boolean. While the focus is on textbox1 the event for F10 will be fired otherwise it won't.
Private Sub txtFileFilter_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtFileFilter.KeyDown
if e.KeyCode = Keys.F10 then
MsgBox(e.KeyCode)
end if
End Sub
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.