SouravKumarDas
SouravKumarDas

Reputation: 3

Closing of all Previous Forms and Opening of New Form in C#

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.

Upvotes: 0

Views: 548

Answers (2)

Aleksa Ristic
Aleksa Ristic

Reputation: 2499

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.

Upvotes: 1

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34152

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();

Upvotes: 0

Related Questions