Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' When the form loads, the KeyPreview property is set to True.
' This lets the form capture keyboard events before
' any other element in the form.
Me.KeyPreview = True
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.Alt And e.KeyCode.ToString = "F" Then
' When the user presses both the 'ALT' key and 'F' key,
' KeyPreview is set to False, and a message appears.
' This message is only displayed when KeyPreview is set to True.
Me.KeyPreview = False
MsgBox("KeyPreview is True, and this is from the FORM.")
End If
End Sub
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.Alt And e.KeyCode.ToString = "F" Then
' When the user presses both the 'Alt key and the 'F' key,
' KeyPreview is set to True, and a message appears.
' This message is only displayed when KeyPreview is set to False.
Me.KeyPreview = True
MsgBox("KeyPreview is False, and this is from the CONTROL")
End If
End Sub