Disabling default spacebar key press event for a button

P

prema askme

Hi

In windows forms, when focus is given to a button, the spacebar is one
of the default keys that can be used to trigger the button. I would like
to override this behaviour.

Does anybody know how I could do this? I still want the user to be able
to click the button with an enter key but just not with the spacebar.

Does the solution involve using the keypreview property? i've read
something similar in an earlier question..

Thanks!
PREMA
 
H

Herfried K. Wagner [MVP]

* prema askme said:
In windows forms, when focus is given to a button, the spacebar is one
of the default keys that can be used to trigger the button. I would like
to override this behaviour.

Does anybody know how I could do this? I still want the user to be able
to click the button with an enter key but just not with the spacebar.

Does the solution involve using the keypreview property? i've read
something similar in an earlier question..

Untested:

\\\
Public Class MyButton
Inherits Button

Protected Overrides Function ProcessDialogKey( _
ByVal keyData As System.Windows.Forms.Keys) As Boolean

If keyData = Keys.Space Then
Return True
Else
Return MyBase.ProcessDialogKey(keyData)
End If
End Function
End Class
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top