I wanted to check if a key was pressed in a form, then perform a subroutine with that key. The code below works only if there are no other controls in the form. What should I do?
Private Sub MainForm_KeyPress1(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
MsgBox(e.KeyChar)
End Sub
You can set the
KeyPreviewproperty of your form toTrue. This way, you’ll catch all the KeyPress events on your form (which will be afterwards handled by the control the keypress happened on, if you didn’t set theHandledproperty of theKeyPressEventArgstoTrue).Source: MSDN