3

谁能帮我理解如何在下面的代码中设置快捷键?它应该是 Alt+X(退出是按钮的名称)。我试过cmd_Exit.text="&Exit"了,但是它打印了“&”并且没有设置快捷键。

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
4

3 回答 3

9

您可以检查是否使用 keydown 事件按下了 Alt+X,然后使用它调用您的退出子:

请注意,您需要在主表单中将 KeyPreview 设置为 True

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
于 2016-10-09T19:39:17.963 回答
1
set Form KeyPreview = True

(事件) Form1_KeyDown

If (e.KeyCode = Keys.F1) Then
    MessageBox.Show("TEST", Button1.Text)
End If
于 2020-01-04T01:09:53.563 回答
0
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
于 2020-01-05T08:06:17.340 回答