Results 1 to 9 of 9

Thread: TextBox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Location
    Carolina, Puerto Rico, USA
    Posts
    227

    Question

    How can i make the TextBox to only accept numbers?


    Thanks in advance!
    NievesJ

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    In a module:
    Code:
    Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    
    Public Const GWL_STYLE = (-16)
    Public Const ES_NUMBER = &H2000&
    
    Public Sub SetNumber(txtNumberText As TextBox, bNumOnly As Boolean)
        Dim lCurStyle As Long
        Dim lNewStyle As Long
    
        lCurStyle = GetWindowLong(txtNumberText.hwnd, GWL_STYLE)
    
        If bNumOnly Then
            lCurStyle = lCurStyle Or ES_NUMBER
        Else
            lCurStyle = lCurStyle And (Not ES_NUMBER)
        End If
    
        lNewStyle = SetWindowLong(txtNumberText.hwnd, GWL_STYLE, lCurStyle)
        txtNumberText.Refresh
    End Sub
    Then just use:
    Code:
    SetNumber Text1, True
    ...to set it only numbers.
    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

  3. #3
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer) 
    'allow for special characters & backspace 
    If Not (IsNumeric(Chr(KeyAscii))) _ 
    And KeyAscii <> 8 Then KeyAscii = 0 
    If KeyAscii < 33 Then Exit Sub 
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  4. #4
    New Member
    Join Date
    Oct 2000
    Location
    Finland
    Posts
    12

    Talking Textbox with numbers-only

    One easy way to do this, is read, what user types and check if it's numeric...

    Code:
    sub Text1_Change...
    on error resume next
    if IsNumeric(text1.text) = False then
    if IsNumeric(Right(Text1.Text,1)) = False then
    Text1.Text = Left(Text1.Text,(Len(Text1.Text)-1))
    Beep
    end if
    end if
    This code checks 'newest' char if textbox. This is working way (i think, didn't test).
    I think, that that API-way is powerful and easy way, and this is slow and no-so-sure way. But if you got nothing, you need to do something =)
    GAMES, PROGRAMS, ACTIVEX CONTROLS!
    ALL, WHAT WE NEED

  5. #5
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    one line baby!



    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If IsNumeric(Chr(KeyAscii)) = False Then KeyAscii = 0
    End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  6. #6
    Guest
    geoff, that will accept numbers, but what if you hit backspace? Nothing will happen. Use this code so you can delete as well.


    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
    Guest
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If KeyAscii > 33 Then If Not (Chr(KeyAscii) Like "#") Then KeyAscii = 0
    End Sub

  8. #8
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    Matthew! C'mon! Who the hell uses backspace?? I dont make mistakes so I don't use it!



    Picky picky picky!

    LOL!

    to much of my thought has been going into my code coloring prog...

    cant think on such simple levels anymore!! LOL

    here...


    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 8 Or KeyAscii = 32 Then Exit Sub Else If IsNumeric(Chr(KeyAscii)) = False Then KeyAscii = 0
    End Sub
    Last edited by Static; Feb 27th, 2001 at 03:33 PM.
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Location
    Carolina, Puerto Rico, USA
    Posts
    227

    Talking

    Thanks a lot everyone!
    NievesJ

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