e.SupressKeyPress ?

ALX

Well-known member
Joined
Nov 16, 2005
Messages
253
Location
Columbia, SC
Programming Experience
10+
I can get hung up on the simplest things! The following code shows a sample of a form's text-box that is screening it's input to limit the allowed data to currency... ($,.0123456789). It also needs to respond if the user presses the "x" key signaling that the user wants to exit or abort the session. All works well except that by inserting the code to handle the "x" keypress, the e.SupressKeyPress method ceases working (for the x key only) and the "x" key gets displayed in the text-box. Is this a MS quirk or am I misunderstanding how the SupressKeyPress method works?

VB.NET:
Public Class Form1

    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _
            Handles TextBox1.KeyDown

        If e.KeyData = 65552 Or e.KeyCode = Keys.Back Or e.KeyCode = Keys.Right Then Exit Sub

        Dim j As Integer = e.KeyValue

        If (j < 48 Or j > 57) And j <> 36 And j <> 44 And j <> 46 Then
            e.SuppressKeyPress = True
            If e.KeyCode = Keys.X Then Exit1()
        End If
    End Sub


    Private Sub Exit1()

        MessageBox.Show("Exit ?")
        '   At this point the "x" character has been displayed in the textbox
        '   in spite of the SupressKeyPress statement in the KeyDown event handler...
    End Sub
End Class
 
Using the X key to exit is a bad idea. It is Windows convention to use Escape for that and such functionality is even built into Windows Forms via the CancelButton property of a Form.

Anyway, if you want to handle currency then you might like to check out this custom control:

Numeric Text Box

Configured correctly, that will allow the user to enter numbers and then automatically display currency formatting when the control loses focus.

With regards to the exit/abort functionality, do you want to handle the X key only when the TextBox has focus or anywhere on the form? If it's the latter then the TextBox doesn't have to care about it. You can simply set the form's KeyPreview property to True and then handle the form's KeyDown event and do this:
If e.KeyData = Keys.X Then
    e.Handled = True
    e.SuppressKeyPress = True
End If
e.Handled prevents the active control from raising a KeyDown event and e.SuppressKeyPress prevents the form and the active control from raising a KeyPress event.
 
Thanks JM. I'll admit, the "x" key for exit is not so conventional. The only reason I'm using it here is because the x key is a keyboard shortcut key to the "EXIT" selection for the dozen or so custom menus used throughout the pgm. It's just a way to stay consistent within this app. The Escape key is used to clear the existing text in the text-box. The research I've done led me to believe that setting SuppressKeyPress to true also sets Handled to true. It just baffled me how all the other keys were being suppressed just fine... except for the x key. If there's no way around this issue, I guess I'll just get rid of the keyboard shortcuts in this sub.
 
Last edited:
Back
Top