TextBox help limit input characters

btisdabomb

Member
Joined
Jul 12, 2006
Messages
16
Programming Experience
Beginner
Is it possible to now allow certain characters to be entered into a textbox while the user is typing them in?
 
Sure, handle the KeyPress event, example to filter out characters ABC:
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
    e.SuppressKeyPress = True
  End Select
End Sub
If you want to go the other way around, just suppress all keys initally and the filter in the keys you want to allow instead.

Select Case is really beneficial here instead of IF ELSE because all the Case statements will automatically list all the keys in the enumeration, they are based on the Select clause KeyCode which represent the Keys enumeration. So if you write "Case " and press key "B" it will automatically display as "Case Keys.B".
 
Back
Top