Replace Text1 with your textbox.Code:Private Sub Text1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Text1.KeyPress
If (IsNumeric(e.KeyChar)) = False Then e.KeyChar = vbNullString
End Sub
Printable View
Replace Text1 with your textbox.Code:Private Sub Text1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Text1.KeyPress
If (IsNumeric(e.KeyChar)) = False Then e.KeyChar = vbNullString
End Sub
Or use the NumericUpDown control, which is a textbox designed to handle numbers.
It's in the same toolbox as the Textbox, Label, Combobox, etc..
i tried it and it didn't work well something is wrong
i used this code and works fine
Code:Private Sub Text1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Text1.TextChanged
If (IsNumeric(Text1.Text)) = False Then Text1.Clear()
End Sub
where is this control???Quote:
Or use the NumericUpDown control, which is a textbox designed to handle numbers.
It's in the same toolbox as the Textbox, Label, Combobox, etc..
The NumericUpDown control serves a different purpose it allows you to move the number in the textbox up and down. Where as using the IsNumeric method allows you to enter numbers only.
In any case it is in the toolbox (NumericUpDown) to be more specific in the "Common Controls" node. Just expand it and it should be mid way up that node ;)
Are negative numbers or decimal numbers (i.e -123 or 123.56) considered numbers? Your solution won't work for these.Quote:
Originally Posted by Zach_VB6
I don't know about the IsNumeric() function, but I do know the NumericUpDown control handles all this gracefully.Quote:
Originally Posted by stanav
Also with the NUD control, you can set a couple of properties to have it look and act just like a TextBox too, so really people need to stop "re-creating" a Numeric Only TextBox, they serve no purpose, and means you have more code to maintain.