-
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
-
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
-
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
-
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.
-
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
-
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
-
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