[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.
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.
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
Re: The user should be allowed to enter only these 3 numbers,1, 2, or 3, in a textbox
Quote:
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.
Re: The user should be allowed to enter only these 3 numbers,1, 2, or 3, in a textbox
Quote:
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
Re: The user should be allowed to enter only these 3 numbers,1, 2, or 3, in a textbox