Textbox only accept certain characters

For example, if you want to textbox2 allow import of only the digits from 1 to 5 try this:

VB.NET:
    Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        If e.KeyChar < Chr(49) Or e.KeyChar > Chr(53) Then e.Handled = True
    End Sub


maybe you will need this:
Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion


I hope that I helped:cool:
 
Another way is this:
VB.NET:
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    Select Case e.KeyCode
        Case Keys.A, Keys.B, Keys.C
            'allowed
        Case Else
            'not allowed
            e.SuppressKeyPress = True
    End Select
End Sub
Keys Enumeration (System.Windows.Forms)
 
Back
Top