如何阻止空格键单击按钮

时间:2012-12-20 01:54:53

标签: vb.net visual-studio-2008 button motorola

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

1 个答案:

答案 0 :(得分:4)

您可以使用Form的KeyPreview属性以及KeyDown事件在传递给按钮之前捕获空格键,如果按钮具有焦点,则可以标记按键处理以防止它冒泡:< / p>

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