Solved

Close all sub windows

Posted on 2010-09-24
6
313 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

16 Experts available now in Live!

Get 1:1 Help Now