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
Solved

Close all sub windows

Posted on 2010-09-24
6
316 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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