How to close all the Forms I want in C#

Asked

Viewed 254 times

1

Let’s say I have 4 presses: form1, form2, form3 and form4. Knowing that the window form1 will always be opened first and when I give a .close() in form2 it, along with the others, must close. How do I achieve this ? Remembering that the form1 should remain open and that this is just an assumption that we have a more general response that helps more people.

  • 3

    Close all you want, what is the difficulty?

2 answers

1

At this point it is worth mentioning how you call the other Forms, so it close according to the call order, below I will cite an example that I use.

Before you go calling any form by the event button_Click, let’s instantiate first at the beginning of our form, first let’s call simply our form_2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Abrindo_e_Fechando_Forms
{
    public partial class Form1 : Form
{
    public Form_2 MeuForm2 = null;
    public Form_3 MeuForm3 = null;
    public Form1()
    {
        InitializeComponent();
    }
    private void btn_abrir_form_2_Click(object sender, EventArgs e)
    {
        Form_2 Form2 = new Form_2();
        Form2.Show();
      }

     }
 }

Now let’s make like that, the Form2 call the Form3, and when you click close on the form2 it closes the form 3

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Abrindo_e_Fechando_Forms
  {
      public partial class Form_2 : Form
   {
    public Form_3 MeuForm3 = new Form_3();

     public Form_2()
     {
          InitializeComponent();
     }

    private void btn_abrir_form_3_Click(object sender, EventArgs e)
    {   

        MeuForm3.Show();
    }

       private void btn_fechar_formAtual_e_aberto_Click(object sender, EventArgs e)
    {
        MeuForm3.Hide();
        this.Close();
    }


  }
}

As you can see I created a new instance of form3 in Form2

public Form_3 MeuForm3 = new Form_3();

In other words, by instantiating Form3, it becomes our object and we can manipulate it in the best way possible. I hope it has helped in some way!

Atte!

1


You can create a control class to store all the menus that you open, where it will store the menu that was opened and what is your "parent" menu, example:

public class MenusAbertos
{
  public Control Menu {get; set;}
  public string ClassePai {get; set;}
}

public class ListaMenusAbertos
{
  public list<MenusAbertos> Menus {get; set;}
}

Then every time before closing a menu, you search on that list if any of them is "son" of that menu that you are closing and if you have children, you close them before and delete from the list.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.