hi.
can anyone here tell me how can i allow certain characters only into a text box.
for instance, a user can only input from these three letter (ABC)
thank you so much.
hi.
can anyone here tell me how can i allow certain characters only into a text box.
for instance, a user can only input from these three letter (ABC)
thank you so much.
Original question: restrict a TextBox so only a tiny set of characters is accepted (example: A, B, C). asked this; pointed toward keystroke filtering and reminded that other keys need handling. The suggestions below expand those ideas and address paste/IME/programmatic input, which simple key trapping can miss.
A practical, robust pattern uses three layers: live keystroke filtering for immediate feedback, a final validation/cleanup step on Leave/Validating to catch paste or input-method edits, and a UI control choice (MaskedTextBox or a dropdown) when the domain is extremely small. In WinForms, setting TextBox.ShortcutsEnabled = False reduces Ctrl+V pastes; to fully block paste, intercept WM_PASTE. For environments where blocking is undesirable, prefer cleaning/normalizing text on validation.
Example — WinForms VB.NET KeyPress filter (keeps control keys usable, blocks other characters not in the allowed set):
' WinForms VB.NET - allow only A, B, C (control keys still pass)
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
Dim allowed As String = "ABCabc"
If Not Char.IsControl(e.KeyChar) AndAlso allowed.IndexOf(e.KeyChar) = -1 Then
e.Handled = True
End If
End Sub Example — final validation (Validating) that removes invalid characters and prevents losing focus until input is cleaned:
Imports System.Text.RegularExpressions
Private Sub TextBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
Dim rx As New Regex("\A[ABC]*\z", RegexOptions.IgnoreCase)
If Not rx.IsMatch(TextBox1.Text) Then
Dim cleaned As New System.Text.StringBuilder()
For Each ch As Char In TextBox1.Text
If "ABC".IndexOf(Char.ToUpperInvariant(ch)) >= 0 Then cleaned.Append(ch)
Next
TextBox1.Text = cleaned.ToString()
e.Cancel = True
End If
End Sub Notes and cautions: test pasting (keyboard and context menu), IME and mobile keyboards, and programmatic text assignments. Decide whether to auto-normalize case or enforce a canonical form. Blocking keystrokes alone is brittle; combine filtering and validation for best UX. Finally, do not rely solely on client-side checks—perform server-side or pre-save validation as well.
Jump to Post— abelingaw 69You can use key trapping by their corresponding ASCII equivalents and Case statements.
Example:
Private Sub Text1_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case 65 To 90, 8, 32, 39 ' A-Z and backspace and space Let these key codes pass through Case 97 To 122, 8 …
You can use key trapping by their corresponding ASCII equivalents and Case statements.
Example:
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 65 To 90, 8, 32, 39 ' A-Z and backspace and space Let these key codes pass through
Case 97 To 122, 8 'a-z and backspace Let these key codes pass through
Case Else
KeyAscii = 0 ' Traps other characters
End Select Try to find the complete ASCII table. Would be useful. Cheers :)
you forgot the enter :D
keyascii 13
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.