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?
 
I assume this must be something to do with the windows handlers for using TAB to jump to next control.
 
Hi Colin,

Yes, the windows handlers grab the tab key and move the cursor to the next control. The KeyPress event does not get fired for tab when tabbing from one textbox to the next. I would like to perform a separate action (close a list box) when the tab key is pressed and then have it continue as normal to the next control. I solved the problem in my program by using the Leave event and in this case it doesn't matter whether it's leaving the textbox because the tab was pressed or leaving the textbox because of a mouse click elsewhere but I still have not been able to capture just the tab key.

Kyle
 
Perhaps there is a way of ordering the firing of events?
But I dont know it!
 
One tip I received from another list is to override the ProcessDialogKey method. Anyone know the synax for doing this?
 
Hi Colin,

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);

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top