473,417 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,417 software developers and data experts.

How to close a dialog form properly when the user presses the escapekey.

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?
Oct 15 '08 #1
7 2894
"Sin Jeong-hun" <ty*******@gmail.comwrote in message
news:24**********************************@a19g2000 pra.googlegroups.com...
>I have a dialog form which pops up from the main window using the
ShowDialog() method. this dialog has no [OK] or [Cancel] button
Why not? If you had a Cancel button your entire problem would be solved.
Oct 15 '08 #2
On 15 Oct, 18:46, Sin Jeong-hun <typing...@gmail.comwrote:
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?
Not a tested solution, but you may try this:

1. Make sure the form's KeyPreview property is set to false. You don't
want to give to the form the chance to process the Keydown event
before its child controls.
2. For each type of control, create a KeyDown event handler.
For example, for comboboxes, you may use the code you wrote above with
a slight change:

if(!ComboBox1.DroppedDown)
{
if (e.KeyCode == Keys.Escape)
{
this.DialogResult =
DialogResult.Cancel;
this.Close();
}
else{
e.Handled = true; //prevent the event
to propagate and close your form
}
}
And make all comboboxes' KeyDown event be handled by this handler.
And so on, for textboxes, etc.

You may also want to create your own controls that inherit from
combobox / textbox, etc that internally deal with Escape key.
This is a more elegant approach because you will not be forced to
create handlers every time you put the controls on another form.
Oct 15 '08 #3
On Oct 16, 2:42*am, "Jeff Johnson" <i....@enough.spamwrote:
"Sin Jeong-hun" <typing...@gmail.comwrote in message

news:24**********************************@a19g2000 pra.googlegroups.com...
I have a dialog form which pops up from the main window using the
ShowDialog() method. this dialog has no [OK] or [Cancel] button

Why not? If you had a Cancel button your entire problem would be solved.
That was because that dialog form wasn't asking the user of something
is OK or not, and it has many controls on it, so there is little room
for unnecessary "Close" button, when there already is a close button
in the window's title bar. But if there is no easy way to detect
orphaned escape keys, maybe I can put a dummy close button outside the
form (invisible location). Thank you, for your reply.
Oct 16 '08 #4
On Oct 16, 7:00*am, nano2k <adrian.rot...@ikonsoft.rowrote:
On 15 Oct, 18:46, Sin Jeong-hun <typing...@gmail.comwrote:


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?

Not a tested solution, but you may try this:

1. Make sure the form's KeyPreview property is set to false. You don't
want to give to the form the chance to process the Keydown event
before its child controls.
2. For each type of control, create a KeyDown event handler.
For example, for comboboxes, you may use the code you wrote above with
a slight change:

* * * * *if(!ComboBox1.DroppedDown)
* * * * *{
* * * * * * * * * * * * *if (e.KeyCode == Keys.Escape)
* * * * * * * * * * * * *{
* * * * * * * * * * * * * * * * *this.DialogResult =
DialogResult.Cancel;
* * * * * * * * * * * * * * * * *this.Close();
* * * * * * * * * * * * *}
* * * * * * * * * * * * *else{
* * * * * * * * * * * * * * * * *e.Handled = true; //prevent the event
to propagate and close your form
* * * * * * * * * * * * *}
* * * * *}

And make all comboboxes' KeyDown event be handled by this handler.
And so on, for textboxes, etc.

You may also want to create your own controls that inherit from
combobox / textbox, etc that internally deal with Escape key.
This is a more elegant approach because you will not be forced to
create handlers every time you put the controls on another form.
Thank you for your detailed answer. But the solution seems to be
complicated for a simple task like just closing the form. I think I
can just put a dummy "Cancel" button to the invisible location of the
form. Like the other reply said, that could simply solve the problem,
though not so clean.
Oct 16 '08 #5
"Sin Jeong-hun" <ty*******@gmail.comwrote in message
news:11**********************************@r37g2000 prr.googlegroups.com...
That was because that dialog form wasn't asking the user of something
is OK or not, and it has many controls on it, so there is little room
for unnecessary "Close" button, when there already is a close button
in the window's title bar. But if there is no easy way to detect
orphaned escape keys, maybe I can put a dummy close button outside the
form (invisible location). Thank you, for your reply.
Personally I would feel very uncomfortable using an application that allowed
the ESC key to function as "I'm done, submit this." It is COMPLETELY
non-standard behavior. But if you're going to go this route, I recommend
that you turn off the TabStop property for the Cancel button so the user
can't accidentally tab to a control that can't be seen.
Oct 16 '08 #6
On Oct 16, 10:13*pm, "Jeff Johnson" <i....@enough.spamwrote:
"Sin Jeong-hun" <typing...@gmail.comwrote in message

news:11**********************************@r37g2000 prr.googlegroups.com...
That was because that dialog form wasn't asking the user of something
is OK or not, and it has many controls on it, so there is little room
for unnecessary "Close" button, when there already is a close button
in the window's title bar. But if there is no easy way to detect
orphaned escape keys, maybe I can put a dummy close button outside the
form (invisible location). Thank you, for your reply.

Personally I would feel very uncomfortable using an application that allowed
the ESC key to function as "I'm done, submit this." It is COMPLETELY
non-standard behavior. But if you're going to go this route, I recommend
that you turn off the TabStop property for the Cancel button so the user
can't accidentally tab to a control that can't be seen.
Thanks for the tip. The form was meant to be closed by clicking on the
close button on the title bar. Users close the form when they don't
need the form any more, not to submit something. Currently, clicking
the close button on the title bar is the only way to close the form,
but I thought it would be more convenient if I can close it with
simple ESC stroke. I don't think "Close" button is always necessary
for forms, when they have the close button on the title bar, it's
redundancy.
Oct 17 '08 #7
"Sin Jeong-hun" <ty*******@gmail.comwrote in message
news:7f**********************************@u40g2000 pru.googlegroups.com...
Thanks for the tip. The form was meant to be closed by clicking on the
close button on the title bar. Users close the form when they don't
need the form any more, not to submit something. Currently, clicking
the close button on the title bar is the only way to close the form,
but I thought it would be more convenient if I can close it with
simple ESC stroke.
Okay, this is making more sense now. Sort of like the Find dialog in the
IDE.

You might consider overriding the ProcessCmdKey() method. In there you first
check to see if the key in question is the Escape key, and then you could
check all your combo boxes to see if any of their DroppedDown properties is
true. If so, you exit the method without processing the key (to let the
combo box handle it itself), otherwise you close the form. That would
probably provide you with the cleaner method of closing the form that you're
looking for.
Oct 17 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Marko Lahtinen | last post by:
Hello! A part of my website is protected with a .htaccess file. Can I somehow bypass the username/password dialog ?? Can I somehow "hardcode" the authentication name and password in my php-file...
1
by: Dave Smithz | last post by:
OK, not strictly a PHP problem I guess but it occurred in my PHP project and I could not find a suitable Javascript NG (under pressure, already extended deadline. I user a "file" input type on a...
4
by: sara | last post by:
I am learning with a simple application/form. I am ok - enter customer name, list box of customers, select a customer, see the items for that customer. The items form has customer ID and Name...
5
by: gilgames | last post by:
I am new in VB.Net, I need some help. I want to resize my dialogs dynamically (so that a dialog made for 1280x1024 screens shows properly on 800x640 screen. I make the calculation in the Handles...
0
by: **Developer** | last post by:
The following is from the docs. But what about if the form executes Me.Close. I experimented a little but can't be sure. Is the Close Method called? I'm not sure what is meant by the Close...
1
by: James | last post by:
CWinFormsDialog in .net 2.0 lets you host a .Net UserControl as a dialog in a managed C++ MDI application. I.e. in the sample: ----------------------------------------------- #include...
1
by: dan.c.roth | last post by:
oForm.Close() vs this.Close() in a modal dialog. oFrom.Close() calls Form.Dispose() but this.Close() ,say in the click event of the Form, does not. I can think of the reason for this but is...
6
by: John | last post by:
In a form how I can I trap for the action that a user opens the built in 'find dialog'? I would like to trap for it and before the dialog shows up, I would like to execute some code, and after that...
1
by: djele | last post by:
Hi, I create and open an Outlook.MailItemClass (opens the Outlook form for wirting an email), and then I am trying to determine when the form is closed (not sent, closed on the X button). I added...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.