Search code examples
c#event-handlingformclosing

C# Closing event handler not working


I wish make the main menu form size to normal when the user closes the current for. I have tried various methods to do with the Closing event however noting seems to work the form still closes and doesn't display the message box, alerting me that the function has run.

Main menu form is open but minimized when the user is using the current form.

My closing event handler;

private void frm_createCust_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            MessageBox.Show("Closing");
            frm_MainMenu frm_MainMenu = new frm_MainMenu();
            frm_MainMenu.WindowState = FormWindowState.Normal;
            frm_MainMenu.Show();
            //this.Close();            
        } 

Thanks in advance.


Solution

  • The child form shouldn't be trying to affect its parent when it is closed, rather the parent should handle that logic and be the one to apply the event handler in its code:

    //somewhere in your main form's code:
    SomeChildForm child = new SomeChildForm();
    child.FormClosing += (s, args) =>
    {
        WindowState = FormWindowState.Normal;
        Show();
    };
    child.Show();