Link to home
Start Free TrialLog in
Avatar of cmdolcet
cmdolcetFlag for United States of America

asked on

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

How can I capture a Tab key press in a Key Press Event Arg in vb 2005?
Avatar of dustock
dustock
Flag of United States of America image

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

Avatar of cmdolcet

ASKER

But its the Key Press Event that is not picking the Keypress even up.
What control are you using the event on?
Its a Key Press Even Args
Are you trying to detect it in a text box, or on the form, or somewhere else?
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.
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

The problem with that is the textbox is created dynamically on the form. Any ideas from that how I can use the PreviewKeyDown event?
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?
ASKER CERTIFIED SOLUTION
Avatar of dustock
dustock
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial