Results 1 to 7 of 7

Thread: limit input to numbers?

  1. #1

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283

    limit input to numbers?

    how do i set the properties of a text box so that the user can only enter numbers?
    thanks,
    marvin

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    use something like this:
    VB Code:
    1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    2.    If Not Char.IsDigit(e.KeyChar) Then
    3.         e.Handled=True
    4.    End If
    5. End Sub
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  3. #3
    Lively Member
    Join Date
    Sep 2003
    Location
    Chicago, IL
    Posts
    64
    That way would allow you to check as they were entered. You could also use regular expressions to validate it in the Validating event. If you didnt want to check every character as it is entered, code something up in the validating event (either a RegEx or something else) and if its not a number (IsNumeric returns false is another way to check) then show a msgbox and set cancel = true.
    Mike Stammer

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by MikeStammer
    That way would allow you to check as they were entered. You could also use regular expressions to validate it in the Validating event. If you didnt want to check every character as it is entered, code something up in the validating event (either a RegEx or something else) and if its not a number (IsNumeric returns false is another way to check) then show a msgbox and set cancel = true.
    lol .

    I wouldn't bother with RE just to allow numbers . The code lunatic3 posted is good . Another way is here :

    VB Code:
    1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    2.  
    3.         'Numeric keys start with 48 through 52
    4.         'backspace key has the ascii code 8
    5.         'period key has the ascii code 46
    6.  
    7.         Dim KeyAscii As Integer
    8.         KeyAscii = Asc(e.KeyChar)
    9.  
    10.         'only disallow numeric values
    11.         Select Case KeyAscii
    12.             Case Asc("0") To Asc("9")
    13.                 'acceptable keystrokes
    14.                 e.Handled = True
    15.                 '   Case Asc(".") if you want to disable
    16.                                   'other keys
    17.  
    18.  
    19.                     'e.Handled = True
    20.             Case Else
    21.                 e.Handled = False
    22.         End Select
    23. End Sub
    I bet on that .

  5. #5
    Addicted Member
    Join Date
    Feb 2002
    Location
    closed
    Posts
    196
    the only problem i can see here is if the user copies n' pastes a string containing numeric chars directly into the textbox you wish to validate. So i would go with something in the text_changed event or suchlike.

    Cheers...

  6. #6
    Lively Member
    Join Date
    Sep 2003
    Location
    Chicago, IL
    Posts
    64
    my thoughts exactly! Thats what validate event is for, to validate things!
    Mike Stammer

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by MikeStammer
    my thoughts exactly! Thats what validate event is for, to validate things!
    Simply Intercept Copy & Paste Messages with API .

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