Results 1 to 10 of 10

Thread: Limiting input type in a textbox....how?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2000
    Posts
    83

    Post

    I want a text box to accept only numbers(0-9) and only x ammount of them like 99999999 then no more are allowed hope this makes sense

    later,
    Iceman

  2. #2
    Hyperactive Member Gimpster's Avatar
    Join Date
    Oct 1999
    Location
    Redmond, WA 98052
    Posts
    331

    Post

    To limit the amount of character that are entered in a textbox, just set the MaxLength property.

    As for limiting the type of character put something like this in the KeyPress event of the textbox

    Code:
    If KeyAscii <> vbKey0 Or KeyAscii <> vbKeyNumpad0 Or ... Then
        Text1.Text = Mid(Text1.Text, 0, Len(Text1.Text) - 1)
    End If
    ------------------
    Ryan

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2000
    Posts
    83

    Post

    There has GOT to be a better way to do that

  4. #4
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523

    Post

    In the KeyPress event you could put:
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If Not IsNumeric(Chr(KeyAscii)) And KeyAscii <> vbKeyBack Then
            KeyAscii = 0
        End If
    End Sub
    And I don't think there is better way to do it. Email me if I'm wrong.
    HTH

    ------------------
    Visual Basic Programmer
    ------------------
    PolComSoft
    You will hear a lot about it.


  5. #5
    New Member
    Join Date
    Feb 2000
    Posts
    15

    Post

    Here's what I use:

    This way it's dynamic and you can use it all over by typing one line of code.

    The validate string value is to be set to what ever character you wish the user to enter.

    Private Sub Text1_KeyPress(KeyAscii As Integer)
    KeyAscii = ValiText(KeyAscii, "0123456789", True)
    End Sub

    Public Function ValiText(KeyIn As Integer, _
    ValidateString As String, Editable As Boolean) As Integer

    Dim ValidateList As String
    Dim KeyOut As Integer
    If Editable = True Then
    ValidateList = UCase(ValidateString) & Chr(8)
    Else
    ValidateList = UCase(ValidateString)
    End If
    If InStr(1, ValidateList, UCase(Chr(KeyIn)), 1) > 0 Then
    KeyOut = KeyIn
    Else
    KeyOut = 0
    Beep
    End If
    ValiText = KeyOut
    End Function


    ------------------

  6. #6
    New Member
    Join Date
    Feb 2000
    Posts
    15

    Post

    Then as far as the amount of the numbers entered just set the Maxlenth = to the desire number of digits you wish to see.

    This is assuming your using a textbox, Richtextbox or any edit box with that property

  7. #7
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Limiting input type in a textbox....how?

    I know you can specify the maxlength, but can you specify a min length for a text field?

  8. #8
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Limiting input type in a textbox....how?

    One way to enforce a minimum length would be to use the Validate event
    VB Code:
    1. Private Sub Text1_Validate(Cancel As Boolean)
    2.  
    3.     If Len(Text1.Text) < 5 Then
    4.         MsgBox "Must be at least 5 digits"
    5.         Cancel = True
    6.     End If
    7.    
    8. End Sub
    And another way to enforce numeric entry in a text box
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
    4.     (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    5.    
    6. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
    7.     (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    8.  
    9. Const GWL_STYLE = -16
    10. Const ES_NUMBER As Long = &H2000&
    11.  
    12. Public Sub SetToNumeric(UText As TextBox, Flag As Boolean)
    13.    '
    14.    '    Allow only numeric input in TextBox
    15.    '
    16.    '    Flag = True - allow numeric only
    17.    '         = False - allow all
    18.    '
    19.    '    Note: This doesn't prevent pasting alpha into the textbox
    20.    '
    21.    Dim curstyle As Long
    22.    Dim newstyle As Long
    23.  
    24.    curstyle = GetWindowLong(UText.hwnd, GWL_STYLE)
    25.  
    26.    If Flag Then
    27.       curstyle = curstyle Or ES_NUMBER
    28.    Else
    29.       curstyle = curstyle And (Not ES_NUMBER)
    30.    End If
    31.  
    32.    newstyle = SetWindowLong(UText.hwnd, GWL_STYLE, curstyle)
    33.    UText.Refresh
    34.    
    35. End Sub
    So to set a textbox to allow numbers only, you would do:
    SetToNumeric myTextBox, True

    and to change it back to allow anything to be entered:
    SetToNumeric myTextBox, False
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  9. #9
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Limiting input type in a textbox....how?

    You can also use Isnumeric() to limit the entry to numeric only

    VB Code:
    1. If IsNumeric(Text1.Text) = False Then
    2.      MsgBox "Type numeric value", , "Result"
    3.      Text1.SetFocus
    4.      Exit Sub
    5. End If
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  10. #10
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Re: Limiting input type in a textbox....how?

    Hang on, has no one thought of using the masked edit box? It acts just like a text box, and you can mask the input so it acts exactl how you wants it to.

    Project > Components > Select "Microsoft Masked Edit Control 6.0" then click ok.

    Then set the mask to 999999999

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