I need code which will give messagebox when user types letter in textbox, when he types number all is ok! Thanks!
Printable View
I need code which will give messagebox when user types letter in textbox, when he types number all is ok! Thanks!
Try this, perhaps?
Note: This does not stop non-numeric characters from being entered, it only displays a messagebox. If you want to delete any non-numeric characters as they are entered, try something like this (as a quick and dirty solution):Code:Private Sub TextBox1_TextChanged(sender as object, e as system.eventargs) _
Handles TextBox1.TextChanged
If Not IsNumeric(TextBox1.Text) and Textbox1.Text <> "" Then
MsgBox("Non-numeric text entered!")
End If
End Sub
Code:Private ValidText as string = ""
Private Sub TextBox1_TextChanged(sender as object, e as system.eventargs) _
Handles TextBox1.TextChanged
If Not IsNumeric(TextBox1.Text) and Textbox1.Text <> "" Then
MsgBox("Non-numeric text entered!")
TextBox1.Text = ValidText
Else
ValidText = TextBox1.Text
End If
End Sub
Alternatively, you can handle the KeyDown event to stop the text being entered into the textbox completely, but that won't stop the user copying+pasting invalid characters into the textbox, so then you'll have to catch that as well.
Use a NumericUpDown control instead of a TextBox