By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 1,800 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

keypressed

P: n/a
vb.net 2003
i have the following procedure

Private Sub StrLicence_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles StrLicence.KeyPress
Select Case e.KeyChar
Case Microsoft.VisualBasic.ChrW(13)
MsgBox(" ENTER PRESSED")
' move to next field
Case Microsoft.VisualBasic.ChrW(8)
MsgBox(" backspace PRESSED")
Case Microsoft.VisualBasic.ChrW(9)
MsgBox("TAB PRESSED")
' move to next field
Case Microsoft.VisualBasic.ChrW(48) To
Microsoft.VisualBasic.ChrW(57)
MsgBox(" NUMBER PRESSED")
Case Else
' show error
MsgBox("MUST BE NUMERIC")
End Select
End Sub

what i want to do is in this case if an alpha charachter is pressed, i show
the must be numeric message, but i also want to stop that character being
displayed ...
Sep 28 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
The procedure I use I've updated from a VB function for KeyAscii.

Here is an example of the call.

Private Sub txtQtyShipped_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles txtQtyShipped.KeyPress

e.KeyChar = NumericChar(e.KeyChar, , True)

End Sub

Public Function NumericChar(ByVal KeyChar As Char, Optional ByVal
AllowNegative As Boolean = False, Optional ByVal AllowDecimal As Boolean =
False) As Char

Select Case Asc(KeyChar)

Case 8, 48 To 57

NumericChar = KeyChar

Case 45

If AllowNegative = True Then

NumericChar = KeyChar

End If

Case 46

If AllowDecimal = True Then

NumericChar = KeyChar

End If

End Select

End Function

"Peter Newman" <Pe*********@discussions.microsoft.comwrote in message
news:51**********************************@microsof t.com...
vb.net 2003
i have the following procedure

Private Sub StrLicence_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles StrLicence.KeyPress
Select Case e.KeyChar
Case Microsoft.VisualBasic.ChrW(13)
MsgBox(" ENTER PRESSED")
' move to next field
Case Microsoft.VisualBasic.ChrW(8)
MsgBox(" backspace PRESSED")
Case Microsoft.VisualBasic.ChrW(9)
MsgBox("TAB PRESSED")
' move to next field
Case Microsoft.VisualBasic.ChrW(48) To
Microsoft.VisualBasic.ChrW(57)
MsgBox(" NUMBER PRESSED")
Case Else
' show error
MsgBox("MUST BE NUMERIC")
End Select
End Sub

what i want to do is in this case if an alpha charachter is pressed, i
show
the must be numeric message, but i also want to stop that character being
displayed ...

Sep 28 '06 #2

P: n/a
Hi Peter,
By using the e.Handled property you can stop the character output to
the textbox.
Also consider using regular expressions for validation.

Select Case Char.IsDigit(e.KeyChar)
Case True
'/ Valid Character Input
Case False
'/ Invalid Character Input
MessageBox.Show("Invalid Character Input")
e.Handled = True
End Select
'/ SAME RESULT BUT USING REGULAR EXPRESSIONS
If System.Text.RegularExpressions.Regex.IsMatch(e.Key Char,
"[0-9]") Then
'/ Valid Character Input
Else
'/ Invalid Character Input
MessageBox.Show("Invalid Character Input")
e.Handled = True
End If

Regards,
Tony

Sep 28 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.