Search code examples
c#.netwinformsformsformclosing

c# cancel application from closing if hidden form has unsaved changes


after searching google and stackoverflow I'm unable to find an answer that helped me in my situation. I've got an application with a start menu(form). when the user presses the X(close) of a (any) form I reload the start menu. Now when that(menu) form gets closed I want to check and notify the user wheter any now hidden forms are in editing mode (with or without unsaved changes) if user presses cancel I want to show that form and stop the application from closing. Now my problem is how do i stop the application from stopping in the code of the other forms. I have an override of the dispose method of the subforms that calls this.close so the ok/cancel messagebox shows but after the MB the start menu closes anyway stopping the program. Should I look for a different method of handling these things or is there a method or eventhandler to modify so this /\ can work?

EDIT: ok here's parts of the code in order of being called. Where does i go wrong?

private void Menu_FormClosing(object sender, FormClosingEventArgs e)
    {
       Global.Forms.Remove(this);
        if (!Global.Clean_Forms())
        {
            e.Cancel = true;
            Global.Forms.Add(this);
        }
    }

public static void Clean_Forms()
    {


        foreach (Form f in Forms)
            {
                if (f is Menu)
                {
                    //do nothing
                }
                else
                {
                    if (!f.IsDisposed)
                    {


                        f.Close();
                    }

                }

            }
        if (Forms.Count != 0)
        {
            isClean = false;
           /* String a = "";
            foreach (Form f in Forms)
            {
                a += f.ToString() + ": ";
            }
            MessageBox.Show(a);*/
        }
        else
        {
            isClean = true;
        }

    }

Yet this doesnt work, application just closes down.

the closing event handler of random form

private void persoon_form_FormClosing(object sender, FormClosingEventArgs e)
    {

        if (editing)
        {
            DialogResult dr;
            dr = MessageBox.Show("uw wijzigingen gaan verloren. Doorgaan?", "sluiten", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.Cancel)
            {
                e.Cancel = true;


            }
            else if (dr == DialogResult.OK)
            {
                Global.size = this.Size;
                Global.position = this.Location;
                Global.Forms.Remove(this);
                Form f = Global.menu();
                f.Show();
                this.Dispose();
            }
        }//somethingelse}

EDIT: @cody gray changed onclose and clean_form still no effect, (shouldn't the messagebox in the Closing event of the subform be shown anyway? cause it doesnt)


Solution

  • I think i figured it out! I changed two things. 1 I made sure that I didn't call this.dispose anywhere as that will result in disposing (not closing) any child forms hence no onclosing event is called. 2. the foreach loop when checking if main menu can close generated collection was modified errors. so with this I made sure it closes in the right order thnx Cody Gray for pointing me in the right direction:

    Stack<Form> stack = new Stack<Form>();
                foreach (Form f in Forms)
                {
                    if (f is Menu)
                    {
                        //do nothing
                    }
                    else
                    {
                        if (!f.IsDisposed)
                        {
    
    
                            stack.Push(f);
                        }
    
                    }
    
                }
                for (int i = 0; i < stack.Count; i++)
                {
                    Form temp = stack.Pop();
                    temp.Close();
                }