Results 1 to 7 of 7

Thread: enhanced text box

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2000
    Posts
    17

    Angry

    im trying to make an "enhanced" textbox control that only accepts numeric input. how would i do that

  2. #2
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    Put a normal textbox on your form and then under keypress event add this (it may/may not work):

    If keyascii => 48 and keyascii <= 57 then
    keyascii = keyascii
    else
    exit sub
    end if

    Its probably a right load o crap but baically u can interseot the keypresses and change the char it displays.

  3. #3
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471

    Exclamation read down....

    Read down a couple of threads. Someone posted that same question a few moments ago. If you want the API I spoke of, let me know.

  4. #4
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471

    Exclamation or try ....

    Psy, your code is fine except for the first part.

    If the keyascii is btw those 2 values, then you have to set keyascii equal to some other value such as 0 then use BEEP to beep the internal speaker and the let the user know he is a moron or something. This is exactly what that api does.....except it looks cooler in doing so

  5. #5
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    Don't forget the Tab, Delete, and Backspace keys.

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        ' Allow characters 0 - 9
        If (KeyAscii < vbKey0) Or (KeyAscii > vbKey9) Then
            ' Allow Backspace, Delete, and Tab keys to be used
            If KeyAscii <> vbKeyBack And KeyAscii <> vbKeyTab And KeyAscii <> vbKeyDelete Then
                KeyAscii = 0
            End If
        End If
    End Sub
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  6. #6
    Guest
    Something more simpler...:

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
         If KeyAscii < 33 Then Exit Sub
         If Not (IsNumeric(Chr(KeyAscii))) Then KeyAscii = 0
    End Sub

  7. #7
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Yep that's the way we like it matthew

    YoungBuck, just to let you know, your code wouldn't work cause of the And's in it.

    Code:
    If KeyAscii <> vbKeyBack And KeyAscii <> vbKeyTab And KeyAscii <> vbKeyDelete Then
    this should be

    Code:
    If Not (KeyAscii = vbKeyBack Or KeyAscii = vbKeyTab Or KeyAscii = vbKeyDelete Or KeyAscii = vbKeySpace) Then
    Please don't see this as a kindof attack, it was just for your educational lessons for today, but anyway, Matthew's code will work best and that's how I always write it

    Sleep well!
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

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