|
-
Nov 29th, 2000, 01:18 PM
#1
Thread Starter
Junior Member
im trying to make an "enhanced" textbox control that only accepts numeric input. how would i do that
-
Nov 29th, 2000, 01:24 PM
#2
Frenzied Member
Put a normal textbox on your form and then under keypress event add this (it may/may not work):
If keyascii => 48 and keyascii <= 57 then
keyascii = keyascii
else
exit sub
end if
Its probably a right load o crap but baically u can interseot the keypresses and change the char it displays.
-
Nov 29th, 2000, 01:24 PM
#3
Hyperactive Member
read down....
Read down a couple of threads. Someone posted that same question a few moments ago. If you want the API I spoke of, let me know.
-
Nov 29th, 2000, 01:30 PM
#4
Hyperactive Member
or try ....
Psy, your code is fine except for the first part.
If the keyascii is btw those 2 values, then you have to set keyascii equal to some other value such as 0 then use BEEP to beep the internal speaker and the let the user know he is a moron or something. This is exactly what that api does.....except it looks cooler in doing so
-
Nov 29th, 2000, 01:40 PM
#5
Fanatic Member
Don't forget the Tab, Delete, and Backspace keys.
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
' Allow characters 0 - 9
If (KeyAscii < vbKey0) Or (KeyAscii > vbKey9) Then
' Allow Backspace, Delete, and Tab keys to be used
If KeyAscii <> vbKeyBack And KeyAscii <> vbKeyTab And KeyAscii <> vbKeyDelete Then
KeyAscii = 0
End If
End If
End Sub
{Insert random techno-babble here}
{Insert quote from some long gone mofo here}
-
Nov 29th, 2000, 04:37 PM
#6
Something more simpler... :
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 33 Then Exit Sub
If Not (IsNumeric(Chr(KeyAscii))) Then KeyAscii = 0
End Sub
-
Nov 29th, 2000, 04:48 PM
#7
Frenzied Member
Yep that's the way we like it matthew 
YoungBuck, just to let you know, your code wouldn't work cause of the And's in it.
Code:
If KeyAscii <> vbKeyBack And KeyAscii <> vbKeyTab And KeyAscii <> vbKeyDelete Then
this should be
Code:
If Not (KeyAscii = vbKeyBack Or KeyAscii = vbKeyTab Or KeyAscii = vbKeyDelete Or KeyAscii = vbKeySpace) Then
Please don't see this as a kindof attack, it was just for your educational lessons for today, but anyway, Matthew's code will work best and that's how I always write it 
Sleep well!
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
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
|