Results 1 to 8 of 8

Thread: Disable Keys

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    2

    Disable Keys

    Hi friends,

    I am new to VB.just now started to coding . I am designing one application in that one text box should allow only (A-Z,0-9,/).when other keys pressed it wont work i.e., i have to lock when other key pressed.

    please guide me and give logic/code to complete above task.

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Disable Keys

    Try this
    vb Code:
    1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    2.  
    3.         Select Case e.KeyChar
    4.             Case "A" To "Z"
    5.                 ' do nothing
    6.             Case "0" To "9"
    7.                 ' do nothing
    8.             Case "\"
    9.                 ' do nothing
    10.             Case Else
    11.                 ' suspend other keys
    12.                 e.KeyChar = ""
    13.         End Select
    14.  
    15.     End Sub



  3. #3

  4. #4
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Disable Keys

    Quote Originally Posted by 4x2y View Post
    Try this
    vb Code:
    1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    2.  
    3.         Select Case e.KeyChar
    4.             Case "A" To "Z"
    5.                 ' do nothing
    6.             Case "0" To "9"
    7.                 ' do nothing
    8.             Case "\"
    9.                 ' do nothing
    10.             Case Else
    11.                 ' suspend other keys
    12.                 e.KeyChar = ""
    13.         End Select
    14.  
    15.     End Sub
    That's not even needed, the only method you'd need to use is:
    Code:
    e.SupressKeypress
    That's it.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  5. #5
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Disable Keys

    Quote Originally Posted by AceInfinity View Post
    That's not even needed, the only method you'd need to use is:
    Code:
    e.SupressKeypress
    That's it.
    There must be some checks because not all keys need to suspended.

    I have used e.KeyChar = "" because e.SupressKeypress is available only under KeyUp/Down events.



  6. #6
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Disable Keys

    Quote Originally Posted by 4x2y View Post
    There must be some checks because not all keys need to suspended.

    I have used e.KeyChar = "" because e.SupressKeypress is available only under KeyUp/Down events.
    That's true, I wasn't referring to his exact code in that statement though, but with your code, I would do something like this, which is a bit more efficient:

    Code:
    Dim MyChars As String = "/" & vbBack
    e.Handled = Not Char.IsLetterOrDigit(e.KeyChar) AndAlso MyChars.IndexOf(e.KeyChar) = -1
    The way you have it set up now, it won't accept input if the char value is lowercase, only uppercase.
    Last edited by AceInfinity; Apr 19th, 2012 at 10:36 PM.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  7. #7

  8. #8
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    Re: Disable Keys

    This is .net code

    The guy asked for API. If you can't give him an API solution, your solution probably isn't wanted. Most people in the API forum have VB6, but want to do the cool things of .net so they use API to get most similar functions.

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