Icet
Icet

Reputation: 688

C# close all forms

I have a problem with my program.

I have 3 Forms: First one opens a second form. Second one opens a third form or returns to the first form. The third form can open the first or second forms.

This is how I open a second Form:

private void Open_second_form()
    {
        Form2 myForm = new Form2(Type_person);
        this.Hide();
        myForm.ShowDialog();
        this.Close();
    }

The rest of the forms I open exactly the same.

Here is a code for how I close forms. Every Form has this method:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (MessageBox.Show("Exit or no?",
                           "My First Application",
                            MessageBoxButtons.YesNo,
                            MessageBoxIcon.Information) == DialogResult.No)
        {
            this.Close();
            Environment.Exit(1);
        }
    }

When I open the third form I get 3 MessagesBoxes. If I open first form I got only 1 MessageBox.

I want to close all forms while getting only one MessageBox.

I tried a lot of solutions but none worked. I tried Application.exit();

Please help me :)

Upvotes: 6

Views: 50781

Answers (8)

gui2
gui2

Reputation: 51

I used this and it worked for me:

for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
        {
            if (Application.OpenForms[i].Name != "FRM01"&&Application.OpenForms[i].Name!= "FRM02")
                Application.OpenForms[i].Close();
        }

Those "FRM" things are the names of my forms, what you can do there is to add as many forms as you like to have as "parents". So each time it iterates it will check if the names of those forms are the ones you wish to keep.

I recommend you add that 'for' into your form closing handler if you wish to control an user from closing parent forms and child forms to still be there.

It is always better to use MDI forms (MDI link here)

Upvotes: 0

Zane Frendin
Zane Frendin

Reputation: 45

Application.Current.Shutdown();

That's what I use whenever I need to close an entire application. It closes all forms, so great for closing everything. I don't know if this is exactly what you want.

Upvotes: 0

Beyondo
Beyondo

Reputation: 3247

Note that this way is faster than Environment.Exit(0);.

WPF

Set your Window Closing Event in (###Window.xaml)

Closing="Window_Closing"

and then in (###WWindow.xaml.cs)

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    foreach(Window w in Application.Current.Windows)
        if(w != this)
            w.Close();
}

Windows Form

  • Go to Form#.cs [Design] >> Form# Properties

  • Then click on Events

  • And double-click on FormClosing

In (Form#.cs)

If you're closing from your Startup form, it's better to use this code.

 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
 {
     for(int i = 1; i < Application.OpenForms.Count; ++i)
         Application.OpenForms[i].Close();
 }

else (Works for any form)

 private void Form7_FormClosing(object sender, FormClosingEventArgs e)
 {
     for(int i = 0; i < Application.OpenForms.Count; ++i)
         if(Application.OpenForms[i] != this)
                Application.OpenForms[i].Close();
 }

Upvotes: 1

Charlston Mabini Vita
Charlston Mabini Vita

Reputation: 174

I'm using this snippet always on my menu form. I hope this helps.

for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
{
    if (Application.OpenForms[i].Name != "Menu")
    Application.OpenForms[i].Close();
}

Upvotes: 5

code4days
code4days

Reputation: 661

If you'd like to close all forms (particularly dialog windows) except for the main form, I've found this function to be useful.

        public static void closeAll()
    {
        FormCollection fc = Application.OpenForms;
        if (fc.Count > 1)
        {
            for (int i = (fc.Count); i > 1; i--)
            {
                Form selectedForm = Application.OpenForms[i - 1];
                selectedForm.Close();
            }
        }
    }

Upvotes: 0

Sherif Hamdy
Sherif Hamdy

Reputation: 593

Call the Environment.Exit(0); method

 private void btnExit_Click(object sender, EventArgs e)
{
    Environment.Exit(0);
}

Upvotes: 4

Nicolas R
Nicolas R

Reputation: 14589

For the form 1, considering the button is called btnForm1To2

private void btnForm1To2_Click(object sender, EventArgs e)
{
    using (Form2 myForm = new Form2(Type_person))
    {
        this.Hide();
        myForm.ShowDialog();
        this.Show();
    }
}

Inside FORM 2, considering the button to page 1 is btnForm2To1, and the button to page 3 is btnForm2To3

private void btnForm2To1_Click(object sender, EventArgs e)
{
    // Just close the form 2, as it was called from form 1, this form will be displayed
    this.DialogResult = DialogResult.OK;
}

private void btnForm2To3_Click(object sender, EventArgs e)
{
    using (Form3 myForm = new Form3(...))
    {
        this.Hide();
        DialogResult form3result = myForm.ShowDialog();

        // Now handle the results from form 3 to navigate to form 2 or 1
        // In my example, form 3 replies Abort to go back to 1. On cancel (or other dialog results) we will stay on form 2
        if (DialogResult.Abort.Equals(form3result))
        {
            this.DialogResult = DialogResult.OK;
        }
        else
        {
            this.Show();
        }
    }
}

Inside FORM 3, considering the button to page 1 is btnForm3To1, and the button to page 2 is btnForm3To2

private void btnForm3To1_Click(object sender, EventArgs e)
{
    this.DialogResult = DialogResult.Abort;
}

private void btnForm3To2_Click(object sender, EventArgs e)
{
    this.DialogResult = DialogResult.OK;
}

And if you want that the user confirms his click with a MessageBox, just place them before the this.DialogResult in each button click method

Upvotes: 2

Sinatr
Sinatr

Reputation: 21969

Your confirmation message is funny and result is unobvious =D

There are 2 solutions possible to your issue.

1) If user chooses to close application - don't display confirmation anymore

private static bool _exiting;

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (!_exiting && MessageBox.Show("Are you sure want to exit?",
                       "My First Application",
                        MessageBoxButtons.OkCancel,
                        MessageBoxIcon.Information) == DialogResult.Ok)
    {
        _exiting = true;
        // this.Close(); // you don't need that, it's already closing
        Environment.Exit(1);
    }
}

2) use CloseReason to confirm only user actions

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.UserClosing)
    {
        if(MessageBox.Show("Are you sure want to exit?",
                       "My First Application",
                        MessageBoxButtons.OkCancel,
                        MessageBoxIcon.Information) == DialogResult.Ok)
            Environment.Exit(1);
        else
            e.Cancel = true; // to don't close form is user change his mind
    }

}

Upvotes: 9

Related Questions