ALX
Well-known member
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