Results 1 to 12 of 12

Thread: TextBox letter disable

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    138

    TextBox letter disable

    How to disable to enter some letters (a,b,c,d...) in textbox? I want to can write just numbers and colon ( in textbox. Thanks.

  2. #2
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    519

    Re: TextBox letter disable

    Maybe a regex match would be easiest for this.

  3. #3
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    307

    Re: TextBox letter disable

    You can use the Keydown Event:

    Code:
    Private Sub Textbox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Textbox1.KeyDown
      If (e.KeyData >= 48 And e.KeyData <= 57) Or e.KeyData = 65722 Then
         MyBase.OnKeyDown(e)
      Else
         e.SuppressKeyPress = True
      End If
    End Sub
    65722 = Colon Key
    48-57 = 0 to 9 keys

  4. #4
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    519

    Re: TextBox letter disable

    Oh yeah, that was a smart idea ^.^
    But are you sure that 65722 is colon?
    In my list of Charcodes, colon is 44

    And japanese colon is 12289

    I just think that 65722 seems so far away :P

  5. #5
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    307

    Re: TextBox letter disable

    That is the e.keydata value. I wrote a little app that has a textbox and 3 labels - one each for keycode, keydata and keyvalue. That way I can easily get the values for each, so that should be right.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    138

    Re: TextBox letter disable

    It doesn't work :'(

    I can enter numbers but can't colon ( : )

    I tried with 58, but it's not.

    Any help?

  7. #7
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: TextBox letter disable

    Check the link in my signature on textbox restrictions.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  8. #8
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: TextBox letter disable

    Bear in mind that the keydown method won't trap if anyone pastes text into the textbox so you'll need to add extra validation before you use the contents if you are just relying on the keydown approach.

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: TextBox letter disable

    Code:
        Private Sub TextBox1_TextChanged(ByVal sender As Object, _
                                         ByVal e As System.EventArgs) _
                                         Handles TextBox1.TextChanged
            For Each ch As Char In TextBox1.Text
                If Not Char.IsDigit(ch) Then
                    If Not ch = ":" Then
                        'bad character
                    End If
                End If
            Next
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: TextBox letter disable

    This is generally a bad approach. You don't need to filter user's input you need to validate it after the user is confirmed it by either clicking some button or pressing the Enter key.

  11. #11
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: TextBox letter disable

    Validating as entering can be OK, but a lot of the time it isn't. I have been wondering if hepeci is working with time values?
    Last edited by dbasnett; Dec 29th, 2009 at 04:31 PM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  12. #12
    Fanatic Member EntityX's Avatar
    Join Date
    Feb 2007
    Location
    Omnipresence
    Posts
    798

    Re: TextBox letter disable

    I just used dkahn's code in post #3 and found it very useful. I modified it like this

    Code:
        '                  0 to 9                     Backspace              0 to 9 on numberpad               mousewheel up, down, PageUp, PageDown, End, Home, left, right, up and down arrows. 
            If (e.KeyData >= 48 And e.KeyData <= 57) Or e.KeyData = 8 Or (e.KeyData >= 96 And e.KeyData <= 105) Or (e.KeyData >= 33 And e.KeyData <= 40) Or e.KeyData = 46 Then
                MyBase.OnKeyDown(e) '                                                                                                                       e.KeyData = 46 for Delete
            Else
                e.SuppressKeyPress = True
            End If
    That way the numberpad digits 0 to 9 are included as well as backspace, mousewheel, and the others mentioned above in the comments. Colon isn't included here.

    I also used dbasnett's code in case someone decides to paste into the combobox(not textbox for my situation) in question.
    Last edited by EntityX; Apr 25th, 2011 at 09:41 PM.
    Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.

    "Persistence is the magic of success." Paramahansa Yogananda

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