Click here to Skip to main content
15,921,884 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
i had use this code
if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }

in key down event of the mdi form


What I have tried:

only working in the single child form other child forms are not closed...
Posted
Updated 31-Mar-18 10:14am
Comments
Richard MacCutchan 29-Mar-18 9:02am    
This is your third post of this problem. I get the feeling your design needs looking at.
Member 11776570 29-Mar-18 9:08am    
now i get the solution. thank u for your feedback

You can add more forms to the list - each form has a 'real' name, like form1, form2, &etc. If you make sure they are in scope than you can go through a list of them in your onclick event (or, in this case, the ESC key).


if (e.KeyCode == Keys.Escape)  {

                form1.Close();
                form2.Close();
                form3.Close();
                form4.Close();

} // if (e.KeyCode == Keys.Escape)


You could, if clever, even create conditions so only the appropriate windows close on this even.


 
Share this answer
 
@User-11743105

There is no need to enable 'KeyPreview in the MDI child forms: just write a 'KeyPreview handler in the MDIParentForm; it will receive key events from all child Forms:
private void MDIParentForm1_KeyDown(object sender, KeyEventArgs e)
{
            if (e.KeyCode == Keys.Escape)
            {
                Control callingControl = sender as Control;

                if (
                    MessageBox.Show(
                        "Really Quit ?",
                        "Quit Application ?", 
                        MessageBoxButtons.OKCancel,
                        MessageBoxIcon.Warning,
                        MessageBoxDefaultButton.Button2,
                        0

                    )
                    == DialogResult.OK
                )
                {
                    // for debugging only
                    if (callingControl != null) Console.WriteLine($"Escape pressed in: {callingControl.Text}");

                    Application.Exit();
                }
            }
        }
    }
}
 
Share this answer
 
v2
it will work
if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }

just change the setting of each
child form
and set
keypreview = true
in each child form setting.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900