Allow NumbersOnly-VBForums
Results 1 to 6 of 6

Thread: Allow NumbersOnly

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,050

    Allow NumbersOnly

    Code:
        Private Sub Text1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Text1.KeyPress
            If (IsNumeric(e.KeyChar)) = False Then e.KeyChar = vbNullString
        End Sub
    Replace Text1 with your textbox.

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    3,817

    Re: Allow NumbersOnly

    Or use the NumericUpDown control, which is a textbox designed to handle numbers.

    It's in the same toolbox as the Textbox, Label, Combobox, etc..

  3. #3
    Lively Member snakegaer's Avatar
    Join Date
    Jul 2007
    Posts
    80

    Re: Allow NumbersOnly

    i tried it and it didn't work well something is wrong

    i used this code and works fine


    Code:
    Private Sub Text1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Text1.TextChanged
            If (IsNumeric(Text1.Text)) = False Then Text1.Clear()
    End Sub
    Or use the NumericUpDown control, which is a textbox designed to handle numbers.

    It's in the same toolbox as the Textbox, Label, Combobox, etc..
    where is this control???

  4. #4
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Allow NumbersOnly

    The NumericUpDown control serves a different purpose it allows you to move the number in the textbox up and down. Where as using the IsNumeric method allows you to enter numbers only.

    In any case it is in the toolbox (NumericUpDown) to be more specific in the "Common Controls" node. Just expand it and it should be mid way up that node

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,238

    Re: Allow NumbersOnly

    Quote Originally Posted by Zach_VB6
    Code:
        Private Sub Text1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Text1.KeyPress
            If (IsNumeric(e.KeyChar)) = False Then e.KeyChar = vbNullString
        End Sub
    Replace Text1 with your textbox.
    Are negative numbers or decimal numbers (i.e -123 or 123.56) considered numbers? Your solution won't work for these.

  6. #6
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    3,817

    Re: Allow NumbersOnly

    Quote Originally Posted by stanav
    Are negative numbers or decimal numbers (i.e -123 or 123.56) considered numbers? Your solution won't work for these.
    I don't know about the IsNumeric() function, but I do know the NumericUpDown control handles all this gracefully.

    Also with the NUD control, you can set a couple of properties to have it look and act just like a TextBox too, so really people need to stop "re-creating" a Numeric Only TextBox, they serve no purpose, and means you have more code to maintain.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.