|
-
Feb 20th, 2003, 09:34 PM
#1
Thread Starter
Hyperactive Member
trap non numeric entry
I’m trying to trap non-numeric entry in a regular text box including decimal point trapping
I put this code on the Keypress Event of the TextBox1 but it doesn’t work!
e.KeyChar = IIf((e.KeyChar >= Chr(48) And e.KeyChar <= Chr(57)) Or e.KeyChar = Chr(8) Or e.KeyChar = Chr(13) Or (e.KeyChar = Chr(46) And InStr(TextBox1.Text, ".") = Chr(0)), e.KeyChar, Chr(0))
-
Feb 20th, 2003, 10:17 PM
#2
Try using the different IsMethods of KeyChar to figure out what it is.
If e.KeyChar.IsNumber then
-
Feb 21st, 2003, 04:47 AM
#3
Hyperactive Member
No better to add a handler.
In fact I created my own custom controls, for numeric only, decimal only and date inheriting from the text box control and adding a handler to check characters enter and disregard any that didn't meet the criteria.
Not near my system at the moment but if you want a code exampple I'll post it here.
-
Feb 21st, 2003, 09:49 AM
#4
Sleep mode
Edneeis's way is perfect but is this what you are looking for :
VB Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim KeyAscii As Integer
KeyAscii = Asc(e.KeyChar)
'only allow numbers, a single decimal point, backspace or enter
Select Case KeyAscii
Case Asc("0") To Asc("9"), Asc(ControlChars.Back)
'acceptable keystrokes
e.Handled = False
Case Asc(".")
e.Handled = False
Case Else
e.Handled = True
End Select
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|