S
Sin Jeong-hun
I have a dialog form which pops up from the main window using the
ShowDialog() method. this dialog has no [OK] or [Cancel] button, and
it has quite a lot of controls on it. Now, I want to close this dialog
form when the user presses the escape key, but that's only when no
control on the form is responsible for the escape key. For example, it
has a ComboBox control, and a user can press the escape key just to
close the drop down list that is being dropped down, not the dialog
form.
I added some code like the following at the dialog form's KeyDown
event handler:
if (e.KeyCode == Keys.Escape)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
The problem is, this close the form, even if when the user pressed the
escape key to close the drop down list, not the form. Of course I
might check if the ComboBox is open like:
if(!ComboBox1.DroppedDown)
{
if (e.KeyCode == Keys.Escape)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
}
But this seems to be only a makeshift, because there can be other
controls responsible for the escape key or that kind of controls can
be added later. What would you recommand in this situation? Is there
any cleaner way for the form to receive only orphaned (not consumed by
any other controls on the form) escape key presses?
ShowDialog() method. this dialog has no [OK] or [Cancel] button, and
it has quite a lot of controls on it. Now, I want to close this dialog
form when the user presses the escape key, but that's only when no
control on the form is responsible for the escape key. For example, it
has a ComboBox control, and a user can press the escape key just to
close the drop down list that is being dropped down, not the dialog
form.
I added some code like the following at the dialog form's KeyDown
event handler:
if (e.KeyCode == Keys.Escape)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
The problem is, this close the form, even if when the user pressed the
escape key to close the drop down list, not the form. Of course I
might check if the ComboBox is open like:
if(!ComboBox1.DroppedDown)
{
if (e.KeyCode == Keys.Escape)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
}
But this seems to be only a makeshift, because there can be other
controls responsible for the escape key or that kind of controls can
be added later. What would you recommand in this situation? Is there
any cleaner way for the form to receive only orphaned (not consumed by
any other controls on the form) escape key presses?