?
Solved

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

Posted on 2014-08-13
10
Medium Priority
?
1,205 Views
Last Modified: 2014-09-15
How can I capture a Tab key press in a Key Press Event Arg in vb 2005?
0
Comment
Question by:cmdolcet
  • 6
  • 4
10 Comments
 
LVL 9

Expert Comment

by:dustock
ID: 40259222
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
 

Author Comment

by:cmdolcet
ID: 40259250
But its the Key Press Event that is not picking the Keypress even up.
0
 
LVL 9

Expert Comment

by:dustock
ID: 40259281
What control are you using the event on?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cmdolcet
ID: 40259286
Its a Key Press Even Args
0
 
LVL 9

Expert Comment

by:dustock
ID: 40259308
Are you trying to detect it in a text box, or on the form, or somewhere else?
0
 

Author Comment

by:cmdolcet
ID: 40259337
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
 
LVL 9

Expert Comment

by:dustock
ID: 40259382
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
 

Author Comment

by:cmdolcet
ID: 40305783
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
 
LVL 9

Expert Comment

by:dustock
ID: 40305849
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
 
LVL 9

Accepted Solution

by:
dustock earned 1200 total points
ID: 40306706
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month17 days, 3 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question