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?
cmdolcetAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dustockCommented:
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

0
cmdolcetAuthor Commented:
But its the Key Press Event that is not picking the Keypress even up.
0
dustockCommented:
What control are you using the event on?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

cmdolcetAuthor Commented:
Its a Key Press Even Args
0
dustockCommented:
Are you trying to detect it in a text box, or on the form, or somewhere else?
0
cmdolcetAuthor 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.
0
dustockCommented:
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

0
cmdolcetAuthor 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?
0
dustockCommented:
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?
0
dustockCommented:
The original post should have included that this would be for dynamic textbox's.  Because I don't know all the detail as to if this app has all dynamic text boxes or some dynamic and some not, and what the end goal is, it makes it difficult to come up with a solution.  That being said I did come up with this with a bit of research.

Once all of the dynamic textbox's have been created and placed on the form (I assume in the load event) put this code in.

        Dim textBoxes = Me.Controls.OfType(Of TextBox)()

        For Each txtBox In textBoxes
            AddHandler txtBox.PreviewKeyDown, AddressOf txtPreviewKeyDown
        Next

Open in new window


Here is the code that the 'address of' will reference

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

Open in new window


When i put my breakpoint on this sub, it captured the tab key as expected and after going through this it jumped into my form1_KeyPress handler, so if you don't already have it, you will want to go to the events for the form and double click on the KeyPress event and put what ever code you want to execute for the TAB key in that event.

If this still doesn't meet your needs please provide the full detail of what you are looking for so that I or other experts can help you reach your desired solution.

Thank you
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.