Results 1 to 7 of 7

Thread: Only allow numbers and . in textbox

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    34

    Only allow numbers and . in textbox

    How to only allow numbers and (.) sign in a textbox?


  2. #2
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    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

  3. #3
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    Pakistan
    Posts
    388

    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.

  4. #4
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Only allow numbers and . in textbox

    Quote Originally Posted by lone_REBEL View Post
    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

  5. #5
    Addicted Member cheesebrother's Avatar
    Join Date
    Jul 2011
    Posts
    153

    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.
    Hooked for good.

  6. #6
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    Pakistan
    Posts
    388

    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.

  7. #7
    Addicted Member cheesebrother's Avatar
    Join Date
    Jul 2011
    Posts
    153

    Re: Only allow numbers and . in textbox

    Yes, many good ways to do a simple task.
    Hooked for good.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width