Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing mdi child with esc

Tags:

c#

winforms

I need to close an mdi child with Esc key. I tried using keydown and keypress events, but i cant even get the form respond to those events when pressing any key.

like image 440
Flezcano Avatar asked Dec 04 '25 06:12

Flezcano


2 Answers

try this

private void Form_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Escape)
    {
        this.Close();
    }
}

or make use of

Form.CancelButton Property - Gets or sets the button control that is clicked when the user presses the ESC key.

like image 56
Pranay Rana Avatar answered Dec 07 '25 00:12

Pranay Rana


Set the Property of the Form KeyPreview=True and go with Keydown Event

if (e.KeyCode == Keys.Escape){
   this.Close();
}
like image 25
RealSteel Avatar answered Dec 06 '25 23:12

RealSteel