Results 1 to 12 of 12

Thread: Textbox that ONLY shows numbers ??

  1. #1

    Thread Starter
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994

    Exclamation Textbox that ONLY shows numbers ??

    How do i make a textbox that you ONLY can write numbers in ?
    I thought of something like this:
    VB Code:
    1. If  txtNumbers.Text = "a" or "A" Then
    2. txtNumbers.Text = ""
    3. End If

    It works fine, but it will just be a hell of a lot of work !
    And i'm... pretty lazy
    Regards
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

  2. #2
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington, DC
    Posts
    422
    Try this:

    VB Code:
    1. Private Sub txtTextBox_KeyPress(KeyAscii As Integer)
    2.  
    3. If (KeyAscii = 8) Or (KeyAscii = 32) Then Exit Sub
    4.  
    5.     If IsNumeric(Chr(KeyAscii)) = False Then
    6.         KeyAscii = 0
    7.         Exit Sub
    8.     End If
    9. End Sub

    I didn't test is, but I think this is what I usually use.

  3. #3
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    look up isNumeric

  4. #4

    Thread Starter
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994

    Thanks

    Thanks dude !
    It works perfectly ! i will look it up laters
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

  5. #5
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629

    Re: Thanks

    Originally posted by vbNeo
    Thanks dude !
    It works perfectly ! i will look it up laters
    until the user paste something into the textbox
    -= a peet post =-

  6. #6
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    This neat snippet will make it the "correct way"

    VB Code:
    1. 'Consts
    2.     Private Const GWL_Style As Integer = (-16)
    3.     Private Const ES_Number As Long = &H2000&
    4.  
    5. 'Declares
    6.     Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    7.     Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    8.  
    9. Public Sub SetNumbersOnly(iTextBox As TextBox, Optional iActive As Boolean = True)
    10.     Dim Temp As Long
    11.    
    12.     'Get the current style
    13.     Temp = GetWindowLong(iTextBox.hWnd, GWL_Style)
    14.    
    15.     'Set the new style
    16.     SetWindowLong iTextBox.hWnd, GWL_Style, IIF(iActive, Temp Or ES_Number, Temp And (Not ES_Number))
    17. End Function
    Last edited by Fox; Aug 13th, 2002 at 03:16 PM.

  7. #7
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    you can still paste nonenumeric data into the textbox
    -= a peet post =-

  8. #8
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    that's why they invented sublcassing
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  9. #9
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    catch any changes via the change event & then run isNumeric

  10. #10
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    actually, that was a little too flip. I don't think isNumeric will work on long strings, and the thing about being able to past non-numeric was a good catch against my solution.

  11. #11
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Muwhahaha!

    In the change event put:

    Text1.Text = Val(Text1.Text)

  12. #12
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Either that or do a val command manually:

    VB Code:
    1. Function NumData(byval StrDat as String) as string
    2. validchrs = "01234567890.-+"
    3. For X = 1 to len(strdat)
    4. if instr(validchrs,mid$(strdat,x,1)) > 1 then newstr = newstr & mid$(strdat,x,1)
    5. Next
    6. NumData = newstr
    7. End Function
    8.  
    9. ' use like:
    10. ' in change event
    11.  
    12. text1.text = numdata(text1.text)

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