Results 1 to 6 of 6

Thread: [RESOLVED] The user should be allowed to enter only these 3 numbers,1, 2, or 3, in a textbox.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    369

    Resolved [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.

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,632

    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.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    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

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    369

    Re: The user should be allowed to enter only these 3 numbers,1, 2, or 3, in a textbox

    Quote Originally Posted by .paul. View Post
    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.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: The user should be allowed to enter only these 3 numbers,1, 2, or 3, in a textbox

    Quote Originally Posted by VS2013 View Post
    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

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    369

    Re: The user should be allowed to enter only these 3 numbers,1, 2, or 3, in a textbox

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width