Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Close all sub windows

Posted on 2010-09-24
6
Medium Priority
?
322 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 320 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 180 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

783 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