If you are looking for an easier way(hack), try my code.
Dim cache As String = ""
Private Sub Textbox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Textbox1.TextChanged
If IsNumeric(Textbox1.Text) = True Then
cache = Int(Textbox1.Text)
Textbox1.text = cache
This code is a bad practice(hack), so you shouldn't really do this unless you are having a hard time on the true solution
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Not Char.IsDigit(e.KeyChar) Then
e.Handled = True
It depends on the type of numbers you are inputting. If whole numbers, I would suggest using the numericupdown control such as suggested above. Other options such as suggested above will be triggered with each key stroke, just of the textbox's validating event might be better since it would not trigger as much and wait for the full input before checking. Also keep in mind if your values contain a decimal point, neither the isnumeric of char.isdigit methods will work.
I think this code will allow to enter numbers and decimal points But it will allow ' , " ; etc
i am still looking for another shortcut way
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If (Char.IsLetter(e.KeyChar) = True) Then
e.Handled = True
Char.IsDigit will return false for decimal points. Especially with using the keypress event where it is not comparing the whole input only the individual character. Example. (Textbox1.text = 1.3) would return 3 results; true-false-true. However this may not be a concern since the original poster hasnt mentioned if it will be whole numbers or not.
I know this way which let you choose the char you want to allow to be written in the textbox
' Allow number 0-9 plus backspace, Del + Home + End will be accepted also
Dim ValidInputChar = "0123456789." + vbBack
If not ValidInputChar.Contains(e.KeyChar) then
Dim TextboxToNumeric as Double = Val(TextBox4.Text)
TextBox4.Text = TextboxToNumeric.ToString("#,###.00")
you just to handle keychar, but you must know the ascii of letters. this following code will not allowed you to input other character except letters or strings. so u cannot input number or other special character.
try this following code :
Private Sub textDate_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles textDate.KeyPress
If (Microsoft.VisualBasic.Asc(e.KeyChar) < 65) _
Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 90) _
And (Microsoft.VisualBasic.Asc(e.KeyChar) < 97) _
Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 122) Then
If (Microsoft.VisualBasic.Asc(e.KeyChar) <> 32) Then
e.Handled = True
If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
e.Handled = False
or more simply use a masked text box from the toolbox rather then a regular textbox & set it to accept only numbers.
There's a piece of code that disables all texts including special characters, it allows number only. I've been using it.
That's of absolutely no help unless you are willing to post the code or a link to it. Also, to everyone who has posted here in the last few days - this thread is three years old. Please do not revive old threads.