|
-
May 20th, 2026, 04:04 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] The user should be allowed to enter only these 3 numbers,1, 2, or 3, in a textbox.
Hi,
The user should be allowed to enter only these 3 numbers,1, 2, or 3, in a textbox. How to restrict him from entering other characters?
Please help.
-
May 20th, 2026, 04:09 PM
#2
Re: The user should be allowed to enter only these 3 numbers,1, 2, or 3, in a textbox
One option is to sell a specialty keyboard with your software that only has those three keys on it.
Or you could just use a numeric up/down control and set the min value to 1 and the max value to 3.
-
May 20th, 2026, 05:24 PM
#3
Re: The user should be allowed to enter only these 3 numbers,1, 2, or 3, in a textbox
Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim validChars() As Char = {"1"c, "2"c, "3"c}
e.Handled = Not (validChars.Contains(e.KeyChar) OrElse Char.IsControl(e.KeyChar))
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 21st, 2026, 04:17 AM
#4
Thread Starter
Hyperactive Member
Re: The user should be allowed to enter only these 3 numbers,1, 2, or 3, in a textbox
 Originally Posted by .paul.
Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim validChars() As Char = {"1"c, "2"c, "3"c}
e.Handled = Not (validChars.Contains(e.KeyChar) OrElse Char.IsControl(e.KeyChar))
End Sub
Thanks for your kind support. Actually, I forgot to mention one thing that the user should enter any single character in that textbox (Ex: 1 or 2 or 3) not 2 characters like 12, 13, 31 etc.
-
May 21st, 2026, 04:26 AM
#5
Re: The user should be allowed to enter only these 3 numbers,1, 2, or 3, in a textbox
 Originally Posted by VS2013
Thanks for your kind support. Actually, I forgot to mention one thing that the user should enter any single character in that textbox (Ex: 1 or 2 or 3) not 2 characters like 12, 13, 31 etc.
Set your TextBox MaxLength property
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 21st, 2026, 04:44 AM
#6
Thread Starter
Hyperactive Member
Re: The user should be allowed to enter only these 3 numbers,1, 2, or 3, in a textbox
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|