0

When I press the Escape key I want to move the focus to the previous control.

How can I achieve this?

  • 2
    what did you tried until this moment ? Code Snippets needed – mihai Jun 19 '13 at 09:47
  • 1
    also, the title is very generic, change it more close to the question – mihai Jun 19 '13 at 09:48
  • 1
    have you read [this](http://stackoverflow.com/help/asking) :) – Jens Kloster Jun 19 '13 at 09:48
  • UserControl cannot get the focus. chekc this thread http://stackoverflow.com/questions/3562235/panel-not-getting-focus/3562449#3562449 – lorenz albert Jun 19 '13 at 09:57
  • 2
    Your user will *never* guess that pressing Escape does anything more than closing the window, dismissing the dialog. It is very unclear why you think you need a non-standard UI like this. Press Shift+Tab instead. This otherwise can be done by overriding ProcessCmdKey() and calling the form's SelectNextControl() method with forward = false. – Hans Passant Jun 19 '13 at 10:05
  • You could set the [tab index](http://msdn.microsoft.com/en-us/library/system.windows.forms.control.tabindex.aspx) – Sayse Jun 19 '13 at 10:13

1 Answers1

1

When you get focus on a control, log the control in a stack so you can always retrieve the previously focussed control.

Next use a keydown eventhandler on the controls and check for the escape key. If escape key is pressed, set focus to the top control in the stack.

Sander
  • 392
  • 2
  • 13