Numbers only in a TextBox

Hi All

I drew a TextBox from the tool box but don't know how to make the TextBox accept numbers only.

Any help is appreciated.





Answer this question

Numbers only in a TextBox

  • littleton.1

    Hi Mac7

    The following should do the trick...


    ' Boolean flag used to determine when a character other than a number is entered.

    Private nonNumberEntered As Boolean = False

     

    ' Handle the KeyDown event to determine the type of character entered into the control.

    Private Sub textBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _

    Handles TextBox1.KeyDown

        ' Initialize the flag to false.

        nonNumberEntered = False

        ' Determine whether the keystroke is a number from the top of the keyboard.

        If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then

            ' Determine whether the keystroke is a number from the keypad.

            If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then

                ' Determine whether the keystroke is a backspace.

                If e.KeyCode <> Keys.Back Then

                    ' A non-numerical keystroke was pressed.

                    ' Set the flag to true and evaluate in KeyPress event.

                     nonNumberEntered = True

                End If

            End If

        End If

    End Sub 'textBox1_KeyDown

     

    ' This event occurs after the KeyDown event and can be used

    ' to prevent characters from entering the control.

    Private Sub textBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _

    Handles TextBox1.KeyPress

        ' Check for the flag being set in the KeyDown event.

        If nonNumberEntered = True Then

            ' Stop the character from being entered into the control since it is non-numerical.

            e.Handled = True

        End If

    End Sub 'textBox1_KeyPress


     


    Hope this helps,
    Steve Hoag
    Microsoft

  • Jack H

    private void searchText_KeyPress(object sender, KeyPressEventArgs e)
    {
     if (e.KeyChar != 8)
     {
      if (char.IsNumber(e.KeyChar) == false)
      {
       e.Handled = true;
    }
    }

    I allow the backspace keypress by checking for character 8. You can easily modify this code to suit your needs.




  • Josh_R

    another option is to handle the on validated event.

    protected override void OnValidating(CancelEventArgs e)

    {

       base.OnValidating (e);

       if(this.Text == String.Empty)

          {

             this.Text = (0.0).ToString();

             return;

          }

       else

       {

          try

          {

                double.Parse(this.Text);

           }

          catch

             {

                e.Cancel = true;

                MessageBox.Show("values must be numeric");

             }

         }

    }
    .
    This way one has the flexibility to allow for a certain kind of numeric input (ie, int only, positive only, within a certain range, etc.) or to more easily allow for scientific notation (like 5.032e-6). One can do these things with the above method also, but it's somewhat less straight foreward than doing it this way. The advantage of the above method over mine is that it gives the user more immediate feedback.



  • JeffJ

    Steve Hoag, you are awesome!  Thank you so much.  I was searching on-line for so long to find the answer to my problem and you saved me. 

    1,000 points for you!

  • Numbers only in a TextBox