This blog describes how to restrict the user's input to letters only, numbers only, letters or numbers only, and special characters only.
Letters only
The below code is restricted to allow only text to be entered into the TextBox.
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Not Char.IsLetter(e.KeyChar) Then
e.Handled = True
End If
End Sub
Numbers only
The below code is restricted to allow only text to be entered into the TextBox.
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Not Char.IsNumber(e.KeyChar) Then
e.Handled = True
End If
End Sub
Letters or numbers only
The below code is restricted to allow Letters or numbers only to be entered into the TextBox special characters are not allowed.
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Not Char.IsLetterOrDigit(e.KeyChar) Then
e.Handled = True
End If
End Sub
special characters only
The below code is restricted to allow special characters only to be entered into the TextBox.
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Char.IsLetterOrDigit(e.KeyChar) Then
e.Handled = True
End If
End Sub

Join the conversation! Your thoughts help the community grow.