Can anyone help me understand how to set a shortcut key in the following code?
It should be Alt+X (Exit is the name of the button).
I tried cmd_Exit.text="&Exit"
, but it printed the "&" and shortcut key was not set up.
Private Sub cmdExit_Click(sender As Object, e As EventArgs) Handles cmdExit.Click
'cmdExit.Capture()
Dim response = MsgBox("Are you sure you want to exit?", CType(MsgBoxStyle.YesNo + MsgBoxStyle.Exclamation, MsgBoxStyle), "Leaving?")
If response = MsgBoxResult.Yes Then 'if yes exit the application
Application.Exit()
End If
End Sub
You can check that Alt+X is being pressed with the keydown event, and then call your exit sub with it:
Note, you will need KeyPreview set to True in your main form
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.X AndAlso e.Modifiers = Keys.Alt Then
e.Handled = True
cmdExit_Click(sender, e) 'or cmdExit.PerformClick()
End If
End Sub