Results 1 to 7 of 7

Thread: Detecting when the tab key is pressed in a textbox

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2012
    Location
    Los Angeles, CA
    Posts
    47

    Detecting when the tab key is pressed in a textbox

    I thought it was simple but i guess it's not...
    How do I detect when the tab key is pressed in a textbox?

    I've tried the following code in KeyPress, KeyDown, Leave, etc. events but the tab is not detected.

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Detecting when the tab key is pressed in a textbox

    Are you asking because you want your textbox to accept a tab press rather than change focus? If so set the TextBox's AcceptsTab property to True.
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  3. #3
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Detecting when the tab key is pressed in a textbox

    Tried what code? you have displayed no code. I assume Tab is it a normal input key.

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2012
    Location
    Los Angeles, CA
    Posts
    47

    Re: Detecting when the tab key is pressed in a textbox

    Here is the code..
    If (e.KeyCode = Keys.Tab) Then
    MessageBox.Show("Tab Key")
    End If

    My default focus is on a textbox which accepts only numeric values. I enter 123456 and hit the tab key to move over to next textbox control. When tab key is hit; I want to run some validations before moving over to the next textbox control.

  5. #5
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Detecting when the tab key is pressed in a textbox

    vb Code:
    1. Private Sub TextBox1_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles TextBox1.PreviewKeyDown
    2.    If e.KeyData = Keys.Tab Then
    3.      e.IsInputKey = True
    4.    End If
    5. End Sub
    6.  
    7. Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    8.    If e.KeyData = Keys.Tab Then
    9.       Debug.WriteLine("Hello tab key")
    10.    End If
    11. End Sub
    Last edited by ident; Jan 27th, 2012 at 05:06 PM. Reason: typo.

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2012
    Location
    Los Angeles, CA
    Posts
    47

    Re: Detecting when the tab key is pressed in a textbox

    Thanks, ident; it worked great.

  7. #7
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Detecting when the tab key is pressed in a textbox

    The TextBox has a Validating event specifically for this.
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

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