Windows Develop Bookmark and Share   
 index > Windows Forms General > Press Esc key to close a form containing many controls
 

Press Esc key to close a form containing many controls

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:

�/span>
btn1.KeyPress += new KeyPressEventHandler(control_KeyPress);
�/span>
btn2.KeyPress += new KeyPressEventHandler(control_KeyPress);
�/span>
btn3.KeyPress += new KeyPressEventHandler(control_KeyPress);
�br>�br>�/span>

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

polocar  Thursday, November 09, 2006 11:15 AM

Do you have a Close/Cancel button on your form? A typical form has a Cancel/OK button. For this button set the DialogResult to the appropriate result. When you do this, clicking the button will close your form.

Then on your form, set the CancelButton property to the button. When you do this pressing the ESC is the same with clicking the Cancel button.

HTH,

Victor

Victor Hadianto  Thursday, November 09, 2006 11:37 AM
That works, so does this:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == Keys.Escape) this.Close();
return base.ProcessCmdKey(ref msg, keyData);
}

nobugz  Thursday, November 09, 2006 11:54 AM

Do you have a Close/Cancel button on your form? A typical form has a Cancel/OK button. For this button set the DialogResult to the appropriate result. When you do this, clicking the button will close your form.

Then on your form, set the CancelButton property to the button. When you do this pressing the ESC is the same with clicking the Cancel button.

HTH,

Victor

Victor Hadianto  Thursday, November 09, 2006 11:37 AM
That works, so does this:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == Keys.Escape) this.Close();
return base.ProcessCmdKey(ref msg, keyData);
}

nobugz  Thursday, November 09, 2006 11:54 AM
Thank you guys, your codes function correctly


polocar  Friday, November 10, 2006 10:18 AM

You can use google to search for other answers

Custom Search

More Threads

• Can someone please help me convert the following C# Calss to VB
• Application Not Responding
• Displaying a Property of an embeded collection in a GridView
• Vector Graphics Editor - GDI+
• RegionMaster Controls Released
• Cannot leave multiple checkboxes unchecked-DataGridViewCheckBoxColumn
• Error creating window handle - out of my control?
• Adding tables to an existing database manually
• report depends on selecting the checkboxes
• Focus problem in MDI