5

I have a WPF form. It handles the KeyUp event and if the released key was Escape, it closes itself.

Also this form has a button to show some Windows Form as a dialog. It does handle the Escape key in the same way.

What happens is that when I press Escape when in the child dialog, both windows close. I expect only the child Windows Forms window to close in this case.

What's the proper way to handle this?

akjoshi
  • 15,374
  • 13
  • 103
  • 121
Mikhail Orlov
  • 2,789
  • 2
  • 28
  • 38

2 Answers2

13

The easiest option for WPF is set the button property IsCancel to true. Then if you press ESC the form would be closed.

akrisanov
  • 3,212
  • 6
  • 33
  • 56
1

Try setting the KeyUp event's handled property equal to true:

private void myDialogForm_KeyUp(object sender, KeyEventArgs e)
{
    e.Handled = true;
}
Seth Moore
  • 3,575
  • 2
  • 23
  • 33