0

How to Close background Forms whenever a new form is opened in Windows Forms C#?

  1. It should not be the specific form to be closed.
  2. It should close all the background forms whenever a form is opened.

2 Answers2

1

Two approaches.

First is using Application.OpenForms like this:

foreach (Form form in Application.OpenForms)
{
    if(Form is YourMainFormClassName) //Check if current form is your main form and do not close it since your app would close. You can use .Hide() if you want
        return;

    form.Close();
}

Other approach is using List but you cannot do List<Form> because when removing you will have problem if you want to remove specific form and you go like yourList.Remove(this) it will remove all items with class of that form. Of course it will happen only if you open one form multiple times but to avoid that we will use Form.Tag property.

An Object that contains data about the control. The default is null

So we will use it to store our Id of the form.

So now when we prepared system let's write it:

First we need List<Form> that is accessible from all classes so we will create it like public static property.

public static class Settings //Class is also static
{
    public static List<Form> OpenedForms = new List<Form>();

    public static int MaxIdOfOpenedForm() //With this method we check max ID of opened form. We will use it later
    {
        int max = -1;
        foreach(Form f in OpenedForms)
        {
            if(Convert.ToInt32(f.Tag) > max)
                max = Convert.ToInt32(f.Tag);
        }
        return max;
    }

    public static void RemoveSpecificForm(Form form) //Remove specific form from list
    {
        for(int i = 0; i < OpenedForms.Count; i++)
        {
            if((OpenedForms[i] as Form).Tag == form.Tag)
            {
                OpenedForms.Remove(form);
                return;
            }
        }
    }

    public static void CloseAllOpenedForms()
    {
        for(int i = 0; i < OpenedForms.Count; i++)
        {
            OpenedForms.Remove(OpenedForms[i]);
        }
    }
}

Now we have list but need to populate it every time we open new form so we will do it like this:

public partial class YourForm
{
    public YourForm()
    {
        InitializeComponents();

        this.Tag = Settings.MaxIdOfOpenedForm() + 1; //We are setting Tag of newly opened form
        Settings.OpenedForms.Add(this); //Adding new form to opened forms.
    }
}

And when we close form we need to remove form from list:

private void YourFormClosed(object sender, EventArgs e)
{
    RemoveSpecificForm(this);
}

and when we set this up just call CloseAllOpenedForms().

This method could have some improvements in performance but this is basic and you expand it further.

Aleksa Ristic
  • 2,394
  • 3
  • 23
  • 54
0

well as only one form can be active and in the foreground, so when openening a new form you can close the previous one:

In your main form:

Form previous_form = null;

and when creating any form:

if( previous_form != null)
    previous_form.Close();
SomeForm someform = new SomeForm();
previsous_form = some_form;
someform.Show();
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
  • Hi I want to close background forms but it should not specify this form or that form to be closed. @Ashkan Mobayen Khiabani – SouravKumarDas Oct 03 '18 at 07:26
  • well you want to close backgronud forms when a new form is opened, when a new form is opened all previous forms will be in background. so opening each form you should close the previous (that will go to the background) – Ashkan Mobayen Khiabani Oct 03 '18 at 07:30
  • But why should i specify the previous form, because it may have any number of form at background...@Ashkan Mobayen Khiabani – SouravKumarDas Oct 03 '18 at 09:01
  • Is there any solution...please send me it. – SouravKumarDas Oct 03 '18 at 09:19
  • @SouravKumarDas If you have more than one Form to close, you need to keep a List of those references somewhere. Or close all forms in `Application.OpenForms` except your starting Form (the one run in `Program.cs`). It doesn't look like a worderful design, though. – Jimi Oct 03 '18 at 09:59