Number Only Input

First of all, Sorry of this is a really simple question. I'm a bit of a noob. Is there anyway to make an input box that will only allow the input of numeric characters Thanks in advance.

EDIT: The following code was taken straight out of help and was used to allow only numbers to be entered into a textbox.

    ' Boolean flag used to determine when a character other than a number is entered.< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

    Private nonNumberEntered As Boolean = False

  

  

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

    Private Sub textBox1_KeyDown(sender As Object, 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(sender As Object, 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

End Class 'Form1

 



Answer this question

Number Only Input

  • Young Joo-MSFT

    yeah that's what i meant. Could you remove the last entered character. not the character at the end of the string. If you don't know how to do this that's cool. I am just nit picking. Like i said they only reason i haven't given up on it yet is because this is a learning exercise and i was just curious.
  • Noli San Jose

    If you're parsing yourself for the number, throw away the maskedtextbox :)

    Yes, it's completely possible. In the event where you're testing to see if it parses as a number, you just need to modify the contents of the textbox.

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

      If Int32.TryParse(TextBox1.Text, Nothing) = False Then

        TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.Text.Length - 1)

      End If

    End Sub

    Of course, is this what you want What if the user had clicked back to the start of the text and added a character at the start of the textbox What if they typed something in the middle of the string



  • prakash1981

    Wait i've figured something out. If you could tell me a way to set the position of the cursor then it will be perfect and i'll post the code.

    EDIT:I meant the position of the cursor in the textbox. not the mouse cursor


  • Jeroen Landheer

    There isn't already done but you can create in a very simple way... could use validator or simply check text in textbox (it must be only "Digit", so you check Char.isDigit(char c )   ).

    You can check char after char or check the whole string (i prefer this)

    string pippo;

    foreach(char c in pippo){

    if(!Char.IsDigit(c))

    //Console.Write("Errore");

    }

     

    sorry it's wrong forum...it's in c#..if it can help you...



  • Ganesh Rangarao

    lol that's cool. Thanks anyways. You've been a great help.
  • Andy Anderson

    Just write some javascript and add it to the attributes of the text box:

    txtInput.Attributes.Add("OnKeyPress", "CheckNumberic()")

  • ChrisFrick

    Yeah, you could, you just need to keep track of it yourself (.net won't tell you what the change was, just that the text was changed:

    Private mLastValue as String

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

      If Int32.TryParse(TextBox1.Text, Nothing) = False Then

        TextBox1.Text = mLastValue

      Else

        mLastValue = TextBox1.Text

      End If

    End Sub



  • Jon_S_

    Sorry, you've reached the end of my knowledge :(

  • KathyA

    There is a magic line of code like below :
    <asp:textbox onkeypress="if ((event.keycode <48) || (event.keycode > 57)) event.returnvalue=false" id=postcode runat=server></asp:textbox>

  • mpaleari

    No, but you _can_ have it only visible when the textbox has the focus - will that do

    You just need to set the HidePromptOnLeave() property to True. It's a stupid name, but it's probably the best you'll get.

    Edit: You could also look at the TextMaskFormat property, which, according to the doco 'Gets or sets a value that determines whether literals and prompt characters are included in the formatted string'



  • Chris Bower

    Hi,

    If it is in web page you can go for valitation controls. If it is in windows form you can get the input from the user and validate for 0-9 by using ASCII comparison.

     

    Regards,

    venkataraman.R


  • Dev Dan

    Thanks geoff. That's what I was looking for. Thanks to everyone else for trying as well but this is the solution that suited me better and it needed to be for vb. One more question. Is there anyway to get rid of that black line thats at the bottom of the textbox Thanks

    EDIT: I figured out hwo to change the prompt character. I tried changing it to a space but it of course leaves spaces in the text box. So, is there anyway to just like disbale prompting


  • Stephen Walters

    Hi,

    There's no magic 'one line of code' that will do it for you, but it's very easy to build your own input box - it's just a new form with a text box, and OK and Cancel buttons.

    Since you're using the UI, instead of validating the string yourself (which you could do as in the previous answer), you could also look into using the MaskedTextBox. The Mask() property of this control allows you define a mask for how text is input. If, say, you only want to allow 10 digit numbers, you simply set a mask of 10 '0's and .net will take care of the rest.

    HTH



  • RobinCurtis

    Ok. That's not rlly how I want it. sorry. Normally i wouldn't bother and i'd just leave it but i am doing this for a bit of a learning exercise. I've already got some code being triggered when the text changes and in that code i've got it checking wether it can parse it as an integer. now if it doesn't i would like it to delete the last character that was entered. is this possible
  • Number Only Input