How do I evaluate textbox input?

How can I evaluate what has just been pasted into a textbox, so that it may only contain numbers or a decimal point, and if contains other characters or letters, the pasting is undone. I would like to know how this can be done without listing every character and letter.

Sorry I forgot to add that the event is:

Private Sub txt_box_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_box.TextChanged

So I can't use e.handled. When I try use undo and then clear undo, I get an overflowstack error.



Answer this question

How do I evaluate textbox input?

  • Wolf-Michael Haberichter

    Hi,
    tReXx's code is fine, but the user must be unable to write "." more than one time, so I edited his code :
    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    Dim LIString As String = TextBox1.Text & "."
    If InStr("1234567890." & Chr(8), e.KeyChar) = 0 Or (e.KeyChar = "." And LIString.IndexOf(".") <> LIString.LastIndexOf(".")) Then e.Handled = True
    End Sub
    Hope this helps.


  • ErinActuateDeveloper

    just set a a string like:

    Dim sString as string = "0123456789" & chr(8)

    then use the keydown event of the textbox

    If InStr(1, sString, e.KeyChar) = 0 Then e.Handled = True

    Done


    same string u can use after pasting.

    just the textbox to chars who aint containd in sstring and use the sstring.remove() methode

  • N. Soltic

    To use IsNumeric function :
    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    If IsNumeric(TextBox1.Text & e.KeyChar) = False And Asc(e.KeyChar) <> 8 Then e.Handled = True
    End Sub



  • Juraj Borza

    Have you considered the IsNumeric property



  • How do I evaluate textbox input?