I am writing a C# application that opens multiple child forms to edit specific records in a database.

When closing a child form, it asks if the user wants to save the data. However, if the main form is closed, all child forms are closed without prompting. It would seem that this would be a simple problem to solve, but I can't seem to figure it out.

I am launching the child windows like this:

private void listResults_DoubleClick(object sender, EventArgs e)
{
    // Open the InfoSheet form for the specific record
    InfoSheet infoSheet = new InfoSheet(Convert.ToInt32(listResults.Items[listResults.SelectedIndices[0]].Text));
    infoSheet.Show();
}

Any suggestions are appreciated!

Recommended Answers

All 6 Replies

when closing the main form its like closing the app...

so a suggestion would be, disable the main form close button when there are child forms open... and just enable it again when there are no child forms open...

or create a global variable(a bool perhaps), that when a child form is open.. its set to true... so when pressing the close button on the main form... it checks this variable if its true it prompts to save.. else it just closes...

>if the main form is closed, all child forms are closed without prompting.

Show/Create Owner form - a form (child) which is owned by other form. Also handle the FormClosing event of Owner form.

Sample,

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.OwnedForms.Length != 0)
            {
                MessageBox.Show("Your message");
                e.Cancel = true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 a = new Form2();
            a.Owner = this;
            a.Show();
        }
commented: perfect solution :) +1

Also, by setting the Owner to your main form it should call the closing events of the child forms when the main form closes.

@adatapost and @Rayshad
I had tried that solution, but forgot to mention why it wasn't desirable. When a form has an owner, it will always appear on top of the owner form, even if it is not selected. This made selecting data from the main form difficult. Sorry I didn't mention it originally.

@phoenix911
Thanks, I ended up doing something along these lines. When the main form is closed, I get a list of all open forms in the application, and try to close them first. If they don't close (the user clicked Cancel on some form close message), I cancel the main form close.

public partial class Form1 : Form
{
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        FormCollection openForms = Application.OpenForms;   // Hold the open child froms
        int openCount;                                      // Count of open forms to determine if a form actually closes
        for (int i = (openForms.Count - 1); i >= 0; i--)    // Loop through all open forms backwards
        {
            if (openForms[i].Handle != this.Handle)         // Make sure we aren't redundantly closing ourself
            {
                openCount = openForms.Count;                // Update the count of open forms
                openForms[i].BringToFront();                // Bring form to the front, so dialogs will make sense
                openForms[i].Close();                       // Close the form
                if (openCount == openForms.Count)           // If the form didn't close (i.e. the user canceled)
                {                                           // (openForms.Count should have decreased by one)
                    e.Cancel = true;                        // Cancel closing this form
                    return;                                 // Go back to what we were doing
                }
            }
        }
    }
}
commented: someone who can help themselves..we like that :) +1

Some thoughts on this:

1) When using multiple forms for 1 application, its advantages to use MDI. Most users like to feel they have control over what they are doing and a bunch of floating forms can be scary if they don't behave the norm. Typically the "Main" form is just a container. and then multiple other forms populate that space with information.

2)in the Main method of the application the static application class's "RUN" method gets called. this is what creates the message loop that keeps your main method from returning until the Message Loop is exited. In your Main method you will see that the run method is taking a new instance of your main form. This means that the message loop is tied to the life of that instance. when you close it. "RUN" returns and the Main method returns.
You could easily create a new form and show it then call and empty RUN() method, then when the forms close the app would stay running until the process was killed externally. Or create a ApplicationContext that would tell the program what to do when a form is closed.

Nice work Dave :)
I was working on code much the same to post until i saw adatapost's solution. Much cleaner if you can use it but if you cant then your solution ensures the open forms are closed.

Good job.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.