3

I am working on a winforms application using c#. I have an MDI container that has a menu on the left and by pushing a button then the appropriate form is visible. If I click for ex 3 times the button which opens Form1 the 6 instances of the form are opened. Thus I thought that I have to write a method that disposes any other Form1 instances. With the following method, I'm looping through the MDI children, but I want some help how to close all other instances except the new one.

  public void DisposeAllButThis(Form form)
    {
        foreach (Form frm in this.MdiChildren)
        {
            if (frm == form)
            {
                frm.Dispose();
                return;
            }
        }
    }
default locale
  • 13,035
  • 13
  • 56
  • 62
user1292656
  • 2,502
  • 20
  • 48
  • 68

7 Answers7

9

You need to check whether the form is of the same type too:

public void DisposeAllButThis(Form form)
{
    foreach (Form frm in this.MdiChildren)
    {
        if (frm.GetType() == form.GetType() 
            && frm != form)
        {
            frm.Dispose();
            frm.Close();
        }
    }
}

For more information on Close and Dispose see: C# Form.Close vs Form.Dispose

Community
  • 1
  • 1
Emond
  • 50,210
  • 11
  • 84
  • 115
2
public void DisposeAllButThis(Form form)
{
    foreach (Form frm in this.MdiChildren)
    {
        if (frm != form)
        {
            frm.Dispose();   
            frm.Close();          
        }
    }
    return;
}
Pankaj
  • 9,749
  • 32
  • 139
  • 283
1
foreach (Form frm in this.MdiChildren)
{
   if (frm != form)
   {
      frm.Dispose();
      return;
   }
} 
Asif Mushtaq
  • 13,010
  • 3
  • 33
  • 42
0

By this fanction You can call it from another class : and note of this : frm.GetType() != Parent.GetType()

public void DisposeAllButThis(Form parentForm)
    {
        foreach (Form frm in Parent.MdiChildren)
        {
            if (frm.GetType() != Parent.GetType()
                && frm != Parent)
            {
                frm.Close();
            }
        }
    }
Reza Paidar
  • 863
  • 4
  • 21
  • 51
0
private void closallforms()
        {
            foreach (Form frm in this.MdiChildren)
            {
                if (frm != Parent)
                {
                    frm.Close();
                }
            }
        }
Dara.Joukar
  • 87
  • 1
  • 4
  • 2
    The answer can become more valuable if you added some explanation and links to documentation, so the original poster and other users can actually learn from it. – Markus Safar Feb 11 '16 at 16:42
0

if you call from another child form you can use: this.DisposeAllButThis(this.FindForm());

and use the method:

private void DisposeAllButThis(Form form)
{
    foreach (Form frm in ParentForm.MdiChildren)
    {
       if (frm != form)
       {
          frm.Dispose();
          frm.Close();
       }
    }
 }
Tom Dhanabhon
  • 441
  • 7
  • 6
0
if (ActiveMdiChild != null)
    ActiveMdiChild.Close();
Samuel Diogo
  • 689
  • 10
  • 13