|
-
Dec 22nd, 2002, 10:15 PM
#1
Thread Starter
Hyperactive Member
Trapping Non Numeric Key
Im trying to trap non numeric key in TextBox but why this code don't Work?
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar < Chr(48) And e.KeyChar > Chr(57) Or e.KeyChar <> Chr(8) Then
e.Handled = True
End If
End Sub
-
Dec 23rd, 2002, 01:28 PM
#2
Registered User
Try this....
If Char.IsNumber(e.KeyChar) Then
or
If Not (Char.IsNumber(e.KeyChar)) AndAlso Strings.Asc(e.KeyChar) <> 8 Then
if it was the other way around......
Last edited by Athley; Dec 23rd, 2002 at 01:31 PM.
-
Dec 23rd, 2002, 02:27 PM
#3
Frenzied Member
If you want to respond only to numerics and back space then your code must be something like this if you want to keep your won format:
you should change the first AND with OR and the OR with AND
If (e.KeyChar < Chr(48) Or e.KeyChar > Chr(57)) And (e.KeyChar <> Chr(8)) Then
-
Dec 23rd, 2002, 03:17 PM
#4
sorry if I'm reposting anything
this works well for me:
VB Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim KeyASCII As Integer = Asc(e.KeyChar)
Dim BACK_SPACE As Integer = 8
If KeyASCII <> BACK_SPACE AndAlso _
KeyASCII < 48 OrElse KeyASCII > 57 Then
' Cancel the entered key, not a valid key
e.Handled = True
End If
End Sub
btw I made my own textbox that accepts numbers only   muhuhuhahahaha
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
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
|