How to only allow numbers and (.) sign in a textbox?
:wave:
Printable View
How to only allow numbers and (.) sign in a textbox?
:wave:
Exjames
You'll probably want to use something like this:
I just copied that "generic" example from MSDN Help.Code:Private Sub Text1_KeyPress (KeyAscii As Integer)
Char = Chr(KeyAscii)
KeyAscii = Asc(UCase(Char))
End Sub
You'll need to modify it to trap for 0123456789(.)
Can you take it from here?
Spoo
Exjames I alrady used that code in the code I uploaded in your calculator topic. But here I write it for you again:
Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
Const allowed_chars As String = "0123456789." 'here add all the characters you want to allow in the textbox
Dim char As String
If KeyAscii = 8 Then Exit Sub 'this will allow the backspace key. if you don't want to allow the backspace key, they remove this line of code
char = Chr(KeyAscii)
If Instr(allowed_chars,char) = 0 Then KeyAscii = 0
End Sub
If you're like me and love small code this works well too. Just like lone_REBEL's it will allow 0-9 "." and backspace (delete also works as well).
Pop that in the KeyPress event and you'll be home free.Code:If Chr(KeyAscii) Like "[0-9]" = False And KeyAscii <> 8 And KeyAscii <> 46 Then KeyAscii = 0
True that, cheese brother! My one lines for the task would be this:
Code:KeyAscii = IIf(KeyAscii = 8 Or KeyAscii = Asc(".") Or (KeyAscii >= Asc("0") And KeyAscii <= Asc("9")),KeyAscii,0)
Yes, many good ways to do a simple task.