How do I prevent user from entering strings other than numeric onto the textbox?

PlayKid

Freshman
Joined
Jun 25, 2005
Messages
38
Dear all,

How do I do that? To prevent user from entering "abcd", the user may only enter "1234".....
Say if the user enter something that's not numeric, then the textbox won't response....

Thanks

PlayKid
 
Code:
  Private Sub txtCalledNumber_KeyPress(ByVal sender As System.Object, ByVal e As KeyPressEventArgs) Handles txtCalledNumber.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not Asc(e.KeyChar) = Keys.Back Then
            e.Handled = True
        End If
    End Sub

kEYS.BACK is for the back space. That way if you make a mistake you can still use the backspace key
 
If you use techmanbd's code and you want the user to be able to enter decimals, then make sure you allow the "." character as well.

Or just skip all the validation and use a NumericUpDown control.
 
Back
Top