Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Close all sub windows

Posted on 2010-09-24
6
Medium Priority
?
321 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

715 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