+ Reply to Thread
Results 1 to 3 of 3

Thread: Textbox only accept certain characters

  1. #1
    Pirahnaplant is offline VB.NET Forum Enthusiast Pirahnaplant is on a distinguished programming path ahead
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Mar 2009
    Posts
    66
    Reputation
    21

    Default Textbox only accept certain characters

    How can I make a text box only accept certain characters?

  2. #2
    danijel.drmic is offline VB.NET Forum Newbie danijel.drmic is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jul 2009
    Age
    25
    Posts
    10
    Reputation
    17

    Default

    For example, if you want to textbox2 allow import of only the digits from 1 to 5 try this:

    Code:
        Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
            If e.KeyChar < Chr(49) Or e.KeyChar > Chr(53) Then e.Handled = True
        End Sub

    maybe you will need this:
    Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion


    I hope that I helped

  3. #3
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Age
    37
    Posts
    11,043
    Reputation
    1563

    Default

    Another way is this:
    Code:
    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        Select Case e.KeyCode
            Case Keys.A, Keys.B, Keys.C
                'allowed
            Case Else
                'not allowed
                e.SuppressKeyPress = True
        End Select
    End Sub
    Keys Enumeration (System.Windows.Forms)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

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