|
-
Nov 21st, 2011, 08:25 AM
#1
Thread Starter
Member
Only allow numbers and . in textbox
How to only allow numbers and (.) sign in a textbox?
-
Nov 21st, 2011, 11:24 AM
#2
Re: Only allow numbers and . in textbox
Exjames
You'll probably want to use something like this:
Code:
Private Sub Text1_KeyPress (KeyAscii As Integer)
Char = Chr(KeyAscii)
KeyAscii = Asc(UCase(Char))
End Sub
I just copied that "generic" example from MSDN Help.
You'll need to modify it to trap for 0123456789(.)
Can you take it from here?
Spoo
-
Nov 21st, 2011, 01:48 PM
#3
Hyperactive Member
Re: Only allow numbers and . in textbox
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
Last edited by lone_REBEL; Nov 21st, 2011 at 01:55 PM.
If your problem is solved, then drag down the Thread Tools and mark your thread as Resolved.
If I helped you solve your problem, inflate some air into my ego by rating my post and adding a comment too.
For notorious issues (elaborate yourself) contact me via PM. I don't answer them in the forums EVER.
-
Nov 21st, 2011, 02:20 PM
#4
Re: Only allow numbers and . in textbox
 Originally Posted by lone_REBEL
Exjames I alrady used that code in the code I uploaded in your calculator topic. But here I write it for you again:
Who knew? 
Nice.
Spoo
-
Nov 22nd, 2011, 10:07 AM
#5
Addicted Member
Re: Only allow numbers and . in textbox
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).
Code:
If Chr(KeyAscii) Like "[0-9]" = False And KeyAscii <> 8 And KeyAscii <> 46 Then KeyAscii = 0
Pop that in the KeyPress event and you'll be home free.
-
Nov 22nd, 2011, 01:39 PM
#6
Hyperactive Member
Re: Only allow numbers and . in textbox
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)
If your problem is solved, then drag down the Thread Tools and mark your thread as Resolved.
If I helped you solve your problem, inflate some air into my ego by rating my post and adding a comment too.
For notorious issues (elaborate yourself) contact me via PM. I don't answer them in the forums EVER.
-
Nov 22nd, 2011, 02:12 PM
#7
Addicted Member
Re: Only allow numbers and . in textbox
Yes, many good ways to do a simple task.
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
|