Results 1 to 3 of 3

Thread: e.SupressKeyPress ?

  1. #1
    ALX
    ALX is offline VB.NET Forum Genius
    .NET Framework
    .NET 4.0
    Join Date
    Nov 2005
    Location
    Columbia, SC
    Posts
    169
    Reputation
    97

    e.SupressKeyPress ?

    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?

    Code:
    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

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is online now VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,332
    Reputation
    1543
    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.

  3. #3
    ALX
    ALX is offline VB.NET Forum Genius
    .NET Framework
    .NET 4.0
    Join Date
    Nov 2005
    Location
    Columbia, SC
    Posts
    169
    Reputation
    97
    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 by ALX; 06-01-2012 at 11:16 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking