Solved

Close all sub windows

Posted on 2010-09-24
6
314 Views
Last Modified: 2012-05-10
I have MainForm and SubForm in project and I create several instances of SubForm from MainForm by using:

SubForm f1 = new SubForm();
f1.Show();

SubForm f2 = new SubForm();
f2.Show();
...
...

Now - what code can I use from MainForm to close all SubForm instances at once?
0
Comment
Question by:I_want_to_know
6 Comments
 
LVL 4

Expert Comment

by:antonybrahin
ID: 33752052
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 80 total points
ID: 33752055
If this an MDI window, or just a standard window? If it's a standard window tehn you would be better off holding the references in a list which you can then iterate over and close the sub windows:
List<SubForm> childWindows = new List<SubForm>();



// open a couple of new windows

childWindows.Add(new SubForm())

childWindows[childWindows.Count-1].Show();



childWindows.Add(new SubForm())

childWindows[childWindows.Count-1].Show();



// close all child windows

foreach(SubForm child in childWindows)

{

    child.Close();

}

Open in new window

0
 
LVL 12

Assisted Solution

by:w00te
w00te earned 45 total points
ID: 33753446
I don't have VS on me right now, but I believe this is a quick solution to close all forms except the one you're currently on.  As long as you dont have multiple forms of other types you wish to keep open, this should be a simple and quick solution.

-w00te
foreach (Form form in Application.OpenForms) 

{ 

    if (form != this)

        form.Close();         

} 

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:I_want_to_know
ID: 33754191
foreach (Form form in Application.OpenForms)
{
    if (form != this)
        form.Close();        
}


This seemed like very quick way but throws exception. I don´t have the exception message in english so I try to translate it: "The collection was edited. The enumeration operation will probably not launched. " Sorry for this clumsy translation. But you will probably know, what exact english message it relates to.
0
 

Author Comment

by:I_want_to_know
ID: 33754250
After little research I found the reason for the exception and solution:

List<Form> list = new List<Form>();

            foreach (Form f in Application.OpenForms)
                list.Add(f);


            foreach (Form f in list)
                if(f !=this)
                f.Close();

Found here: http://www.csharp411.com/close-all-forms-in-a-thread-safe-manner/
0
 

Author Closing Comment

by:I_want_to_know
ID: 33754314
Both had some useful information
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
A short film showing how OnPage and Connectwise integration works.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now