Press Esc key to close a form containing many controls

P

polocar

Hi,
I would like to find a way in C# so that, when the user presses the
"Esc" key, the form closes; the problem is that I have a lot of
controls in the form.
At the beginning I have tried with the following form event handler:

protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);

if (e.KeyChar == (char)Keys.Escape)
Close();
}

But I have realized that it functions only if there are no controls in
the form.

Suppose that you have three Buttons in the form (btn1, btn2 and btn3).
The fastest way (with the minimum number of code lines) I have found to
close the form pressing the Esc key is the following one:

....
btn1.KeyPress += new KeyPressEventHandler(control_KeyPress);
....
btn2.KeyPress += new KeyPressEventHandler(control_KeyPress);
....
btn3.KeyPress += new KeyPressEventHandler(control_KeyPress);
....
....
....

void control_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Escape)
Close();
}

Have I to add a

Control_i.KeyPress += new KeyPressEventHandler(control_KeyPress);

statement for EVERY control in the form??? (suppose you have 100
controls... It's not so nice!!!)

Do you know if there is a faster way (with a lower number of code
lines) to do that?

Thank you very much
 
T

Tom Porterfield

Hi,
I would like to find a way in C# so that, when the user presses the
"Esc" key, the form closes; the problem is that I have a lot of
controls in the form.
At the beginning I have tried with the following form event handler:

protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);

if (e.KeyChar == (char)Keys.Escape)
Close();
}

But I have realized that it functions only if there are no controls in
the form.

Suppose that you have three Buttons in the form (btn1, btn2 and btn3).
The fastest way (with the minimum number of code lines) I have found to
close the form pressing the Esc key is the following one:

...
btn1.KeyPress += new KeyPressEventHandler(control_KeyPress);
...
btn2.KeyPress += new KeyPressEventHandler(control_KeyPress);
...
btn3.KeyPress += new KeyPressEventHandler(control_KeyPress);
...
...
...

void control_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Escape)
Close();
}

Have I to add a

Control_i.KeyPress += new KeyPressEventHandler(control_KeyPress);

statement for EVERY control in the form??? (suppose you have 100
controls... It's not so nice!!!)

Do you know if there is a faster way (with a lower number of code
lines) to do that?

Easiest way to do this is to make one button the close button. Get rid of
all of your keypress event handlers. In your close button's click event
handler, close the form. Then go to the properties for the form and set
your close button as the cancel button for the form.
 
M

Martijn Mulder

I would like to find a way in C# so that, when the user presses the
"Esc" key, the form closes; the problem is that I have a lot of
controls in the form.


Call System.Windows.Forms.Application.Exit() in response to the Escape key
and your program terminates. Or do I misinterpret your question and do you
only want to close one form without closing the program?
 
H

Hans Kesting

Hi,
I would like to find a way in C# so that, when the user presses the
"Esc" key, the form closes; the problem is that I have a lot of
controls in the form.
At the beginning I have tried with the following form event handler:

protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);

if (e.KeyChar == (char)Keys.Escape)
Close();
}

But I have realized that it functions only if there are no controls in
the form.

Suppose that you have three Buttons in the form (btn1, btn2 and btn3).
The fastest way (with the minimum number of code lines) I have found to
close the form pressing the Esc key is the following one:

...
btn1.KeyPress += new KeyPressEventHandler(control_KeyPress);
...
btn2.KeyPress += new KeyPressEventHandler(control_KeyPress);
...
btn3.KeyPress += new KeyPressEventHandler(control_KeyPress);
...
...
...

void control_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Escape)
Close();
}

Have I to add a

Control_i.KeyPress += new KeyPressEventHandler(control_KeyPress);

statement for EVERY control in the form??? (suppose you have 100
controls... It's not so nice!!!)

Do you know if there is a faster way (with a lower number of code
lines) to do that?

Thank you very much

You can (recursively) loop though all controls on the form. When you
find one that can take a KeyPress event (particular baseclass??),
attach the handler.

Hans Kesting
 
P

polocar

Thank you guys.
Someone suggested me that another possible solution is to set the form
KeyPreview property to true:

this.KeyPreview = true;

so that the form can handle the keyboard events before its controls.

Then you can use the OnKeyPress form event handler...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top