2012-12-20 84 views
1

我正在使用VS 2008.在Windows或智能設備項目中,當按鈕當前具有焦點時按空格鍵將導致按鈕被點擊,它會提高Click事件。是否有任何屬性或設置會導致它跳過它,使空格鍵不會導致按鈕單擊?如何停止單擊按鈕空格鍵

回答

4

您可以使用窗體的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 
+0

它仍然有按鈕動作。 – soclose