Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capturing tab key

Status
Not open for further replies.

kyledunn

Programmer
Jan 2, 2001
145
US
I have tried the KeyDown, KeyPress and KeyUp events but they don't capture the pressing of the Tab key. Anyone know of how to capture the event when the Tab key is pressed?
 
what value are you trying to capture? By that i mean, in the Key Press sub, you could do:
If argument = number-corresponding-to-key then
blah blah

Does the Tab key have a value assigned to it?

Jack
 
The tab key is ASCII character 9. The KeyPress event will not capture it when exiting a textbox unless the AcceptsTab=true and Multiline=true but then tab does not actually leave the textbox, it becomes part of the text. I was able to solve the programming problem by using the Leave event which fires whether you are pressing tab or clicking in another control when leaving the textbox. This worked fine for the purpose I needed but I did not specifically capture the tab key. The tab is being used by the windows handler to move from control to control. I just wanted to capture it, do something, and then have the cursor continue to the next control. I have been advised that overriding the ProcessDialogKey method will give me access to all the keys that are typed but I don't know the proper syntax to use that approach yet. Do you know the syntax for overriding the ProcessDialogKey method?

Kyle
 
I think all you have to do to override any function/sub is just include the word "override" when declaring it
so like:
Public Override ProcessDialogKey()

But I havn't really looked into that much. We tried overriding the dispose method to try and take control of the garbage control, but we didn't have any success with it.

Jack
p.s how was your Christmas?
 
I would expect the ProcessDialogKey() to have at least one argument, the key that is being processed. If I find the answer I'll post here for all of us.

Christmas was great, one of the best for us. Everyone in the family was happy. The company I am working for is purchasing a new laptop for me complete with docking station and duel monitors, 48 Gig HD, DVD, etc. Pretty cool getting a development laptop. That's going to make my Christmas! Hope you had a good one too.
 
Sweet! Sounds like you may be looking forward to going back to work, eh?
:)

yeah, keep me posted on that processdialogkey() thing. If I see anythign I'll let you know too.

jack
 
Hi guys,

I searched everywhere I could think of and couldn't find the syntax for overriding the ProcessDialogKey method. I finally looked at online help and it turned out to be really simple.

Here's the story:

In the online help it shows this syntax:

[C#]
protected override bool ProcessDialogKey(Keys keyData);

[Visual Basic]
Overrides Protected Function ProcessDialogKey(ByVal keyData As Keys) As Boolean

Here is the C# version, I'll leave the VB version to you.

In the C# version you create the method as follows:

protected override bool ProcessDialogKey(Keys keyData)
{
MessageBox.Show("Override: " + keyData);
base.ProcessDialogKey(keyData);
return true;
}

This method intercepts and processes keys that are not intercepted by the KeyPress event.

base.ProcessDialogKey(keyData) takes the keyData and processes it as if it had not been intercepted. For example with this override in place if you press the tab key when your cursor is in a text box the override method will fire, the MessageBox will show and then the base.ProcessDialogKey will continue with normal operation of the tab moving you to the next control. If you press the shift key, as you would expect the override fires but the base process leaves the cursor in the text box.

This all seems so simple once you know it and such a mystery when you don't.

One more mystery put to rest.

Kyle


 
Hang on Sherlock!
;)

I can see how that can work for a VB app on the client computer, but what about an ASP.NET page where a keystroke is a client side event? Wouldn't you need to have the page post back to the server each time a key is pressed to see if it was the tab key or not?

Also, I didn't catch why the tab key would trigger it to fire, but the shift key wouldn't.

ARRGHH! Me thinks I've created MORE mysteries for us to solve! Hey, btw, do you do most of your coding with C# or VB?

jack
 
Sorry Jack, right now I am writing a desktop application and I was very excited about overriding a method. It was my first override. Doesn't take much to get me excited. I don't know how I would or if I could accomplish in a web page what I have written for the desktop. There is some overlap between Windows Forms and ASP.NET that makes this override concept useful because of the server side capabilities of ASP.NET but I should have qualified that I was not writing an ASP.NET page. Thanks for creating a new mystery of getting the custom user input control I have for the desktop to work with all the same features in a browser.

Both the tab key and the shift key do fire the overrided ProcessDialogKey method. My test showed that the tab key continued with normal behavior being processed with the base.ProcessDialogKey(keyData) taking the cursor to the next control while the shift key also continued with normal behavior after being processed with the base.ProcessDialogKey(keyData) by remaining in the text box. If the base.ProcessDialogKey(keyData) is not included in the override ProcessDialogKey(Keys keyData) method then the override ProcessDialogKey(Keys keyData) consumes the keystrokes and the tab does not move the cursor to the next control and I suspect the shift key is consumed as well.

I do all of my coding in C#. I have done some VB programming over the years but my primary language for many years was C. Of course all previous languages were procedural and this oop is new and exciting to me. I can interpret VB if I have to but I have settled on C# as my primary language.

Kyle
 
Example for capturing the Tab keypress:

Private Declare Function GetKeyState% _
Lib "User32" (ByVal nVirtKey%)

Private Sub Text1_LostFocus()
If GetKeyState(vbKeyTab) < 0 Then
Text1.SetFocus
MsgBox &quot;TAB was pressed&quot;
End If
End Sub

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top