views:

102

answers:

3

Normally when pressing the TAB key you change the focus to the next control in the given tab order. I would like to prevent that and have the TAB key do something else. In my case I'd like to change focus from a combobox to a completely different control. I can't do this by setting the tab order. I need to do this programatically. Any idea how? It seems like the KeyDown and KeyPress events can't handle TAB key correctly. Thanks.

A: 

Override the control's LostFocus event see link below for examples:

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.lostfocus.aspx

Adrian
+1  A: 

You can try this code on your KeyDown event:

if (e.KeyCode == Keys.Tab) {
  //your logic
  e.SuppressKeyPress();
}

If the button clicked is Tab, then do any custom logic you want, then call SuppressKeyPress to stop the KeyPress event from firing and invoking the normal Tab logic for you.

Øyvind Bråthen
+1  A: 

Override ProcessDialogKey or ProcessTabKey on your Form and do the logic you want depending on which control is focused.

liggett78