|
-
Mar 16th, 2009, 09:51 AM
#1
Thread Starter
Addicted Member
[2005] Error on typing word
I need code which will give messagebox when user types letter in textbox, when he types number all is ok! Thanks!
-
Mar 16th, 2009, 10:16 AM
#2
Junior Member
Re: [2005] Error on typing word
Try this, perhaps?
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
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 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.
Last edited by Naigewron; Mar 16th, 2009 at 10:22 AM.
-
Mar 16th, 2009, 10:29 AM
#3
Re: [2005] Error on typing word
Use a NumericUpDown control instead of a TextBox
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|