Disable Enter as button click

Karlman

Member
Joined
Nov 25, 2009
Messages
8
Programming Experience
10+
Is there a way to prevent the Enter key from being interpreted as a button click.

I am trapping key strokes at the form level but if a button has the focus and the ENTER key is pressed I cannot trap the enter because the button consumes it as a button click event. I am also unable to trap the enter key in any of the button's key events.

I am interfacing with magtripe swipe and barcode readers that send their information as keystrokes with a CRLF at the end. I have a work around by chaning the focus to a textbox anytime keystrokes are detected but I am hoping for better control.

Is there a way to disable the buttons ability to do this?

Thank you
 
You'll need to create your own custom control that filters out the Enter key. It's very simple:
VB.NET:
Public Class ButtonEx
    Inherits Button

    Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
        Return keyData = Keys.Enter OrElse MyBase.ProcessCmdKey(msg, keyData)
    End Function

End Class
Just add that class to your project and build and you can then add your new ButtonEx control to your forms from the Toolbox. If you already have Button controls on a form you can simply open the designer code file and change System.Windows.Forms.Button to ButtonEx in the code. To see the designer code files in the Solution Explorer you must click the Show All Files button.
 
Back
Top