Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Press Esc key to close a form with many controls 2

Status
Not open for further replies.

polocar

Programmer
Sep 20, 2004
89
IT
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:

Code:
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:

Code:
…
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

Code:
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
 
Their is an easy way to achieve this:

Set the forms KeyPreview property to true.
Forms provide a Boolean KeyPreview property. If you set this to true, your form receives keypress
events when any of its controls have focus, and it receives these events before the
control does.[/color blue]


Add the following code:
Code:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)Keys.Escape)
    {
        MessageBox.Show("You 've hit the Escape button !");
[COLOR=green]
        // If you don't want your control to receive the escape button, 
        // set the handled property to true:[/color green]
        e.Handled = true;
    }
}

The design part of your form:
Code:
this.KeyPreview = true;
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);

Greetz,

Geert
 
That is invaluable. I'll give you a star even though it wasn't my question. :)

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Uhhh - from what I read : that's exactly what your question was.

By previewing every keypress you don't have to worry about capturing the events of other controls. The work is done with you.

Go Geert.
 
Look at the name of the original poster.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
now where is that "Delete Post" button again? lol

My mistake!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top