We help IT Professionals succeed at work.

How to capture a TAB key press in a Key Press Event

cmdolcet
cmdolcet asked
on
4,078 Views
Last Modified: 2014-09-15
How can I capture a Tab key press in a Key Press Event Arg in vb 2005?
Comment
Watch Question

Commented:
Use an if to see if it was the tab key

    If e.KeyCode = Keys.Tab Then
        'run you code
    End If

Open in new window

Author

Commented:
But its the Key Press Event that is not picking the Keypress even up.

Commented:
What control are you using the event on?

Author

Commented:
Its a Key Press Even Args

Commented:
Are you trying to detect it in a text box, or on the form, or somewhere else?

Author

Commented:
I have a textbox on the form, however any other key I press is detected (BACKSpae,Enter,Shift, and letter or numerical keys) just not the TAB key.

Commented:
Use the PreviewKeyDown event, and add in if block that I have in the example below.  The PreviewKeyDown will capture the tab key and the isinputKey = true will treat is like a normal key press an not like the normal function of tab in a form.  Once that event fires it will fire the keypress event next.  Hope this helps.

    Private Sub TextBox1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles txtQty1.PreviewKeyDown
        If e.KeyCode = Keys.Tab Then
            e.IsInputKey = True
        End If
    End Sub

Open in new window

Author

Commented:
The problem with that is the textbox is created dynamically on the form. Any ideas from that how I can use the PreviewKeyDown event?

Commented:
How did you accomplish this before with the keypress event?  You stated in previous comments that the keypress event was picking up other keys for the text box?
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.