2

我正在使用 VS 2008。在 Windows 或智能设备项目中,当按钮当前具有焦点时按空格键将导致单击按钮并引发Click事件。是否有任何属性或设置会导致它跳过它以使空格键不会导致按钮单击?

4

1 回答 1

5

您可以使用 Form 的 KeyPreview 属性和 KeyDown 事件在空格键传递给按钮之前捕获空格键,如果按钮具有焦点,则可以将按键标记为已处理以防止其冒泡:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MessageBox.Show("Button Clicked")
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.KeyPreview = True
End Sub

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
    If e.KeyCode = Keys.Space And Button1.Focused Then
        e.Handled = True
    End If
End Sub
于 2012-12-20T02:07:48.030 回答