|
-
Apr 2nd, 2001, 03:55 PM
#1
Thread Starter
Addicted Member
http://www.vbsquare.com/tips/tip317.html
I've used this tip before without any problem, but could someone explain how to expand this to allow characters like the return or the period key to not be blocked out?
Also, is there a way to stop it from causing a beep on bad character input?
Also a general explanation of what is going on would be helpful. I understand that some sort of bit masking is occuring, but I don't understand enough about the WindowLong parameter to fully understand what is going on.
Thanks in advance,
Dim A
-
Apr 2nd, 2001, 04:29 PM
#2
Addicted Member
um
i don't really understand what you mean, here's what i think you could do.
Code:
Private Sub Text1_Change()
'this will block periods and 8's
Text1.Text = Replace(Text1.Text, ".", "")
Text1.Text = Replace(Text1.Text, "8", "")
End Sub
that's what i'd use, if you have to block a lot of characters, you could use the method stated above, it's be a lot slower, but it makes more sense to me
-
Apr 2nd, 2001, 04:36 PM
#3
Thread Starter
Addicted Member
Basically...
I want to block all alphabetic characters, and characters that wouldn't show up in a decimal number. ( only allow[0123456789-.]), but I'd also like to allow return so I don't get error beeps, and since I'm using return to go to the next text entry field. I could disallow based on each individual character, but If I understood the windowLong value better, I may be able to just set bit flags until I get what I want...
- Dim A
-
Apr 3rd, 2001, 12:31 PM
#4
Monday Morning Lunatic
In your keydown event (or something, don't have VB here to test with):
Code:
If Not Chr$(KeyAscii) Like "[0-9]" or Chr$(KeyAscii) <> "-" or Chr$(KeyAscii) <> "." Then
KeyAscii = 0
End If
Just extend that list to keep return, escape, etc.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 3rd, 2001, 02:44 PM
#5
New Member
IsNumeric
Have a look at the IsNumeric() function in the MSDN help. From MSDN library:
IsNumeric
Returns a Boolean value indicating whether an expression can be evaluated as a number.
Syntax
IsNumeric(expression)
The required expression argument is a Variant containing a numeric expression or string expression.
Remarks
IsNumeric returns True if the entire expression is recognized as a number; otherwise, it returns False.
IsNumeric returns False if expression is a date expression.
Arcadia
"Computers aren't clever, they
only think they are" - Anon
-
Apr 3rd, 2001, 02:46 PM
#6
Add these for the top 2 lines. This way, it'll make sure that only 1 decimal of 1 negative sign can be entered.
Code:
If Chr$(KeyAscii) = "." And Text1 Like "*.*" Then KeyAscii = 0: Exit Sub
If Chr$(KeyAscii) = "-" And Text1 Like "*-*" Then KeyAscii = 0: Exit Sub
-
Apr 3rd, 2001, 10:19 PM
#7
Code:
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If Not IsNumeric(Chr(KeyCode)) Then
'190 is dot. To get the keycode of each key, put a debug.print keycode here
If KeyCode <> 190 Then Text1.Locked = True
Debug.Print KeyCode
End If
'add additional checks for all other chars you want to keep
End Sub
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
Text1.Locked = False
End Sub
Last edited by Lord Orwell; Apr 3rd, 2001 at 10:26 PM.
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
|