Results 1 to 7 of 7

Thread: Number Only textboxes?

  1. #1

    Thread Starter
    Addicted Member Dim A's Avatar
    Join Date
    Jul 2000
    Posts
    201
    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

  2. #2
    Addicted Member
    Join Date
    Feb 2001
    Location
    Upstate NY
    Posts
    210

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

  3. #3

    Thread Starter
    Addicted Member Dim A's Avatar
    Join Date
    Jul 2000
    Posts
    201

    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

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  5. #5
    New Member
    Join Date
    Mar 2001
    Location
    Surrey, England
    Posts
    3

    Lightbulb 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

  6. #6
    Guest
    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

  7. #7
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628
    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.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

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