Click here to Skip to main content
16,021,169 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
I would like to close all opened child forms in MDI form in c#.net can anyone help
Posted

Is it so difficult to look at the MSDN documentation page on a class and find the method Close?

The problem is: why MDI? How much longer people are going to get into it? Who needs MDI, ever? Why torturing yourself and scaring off your users?
Do yourself a great favor: do not use MDI at all. You can do much easier to implement design without it, with much better quality. MDI is highly discouraged even by Microsoft, in fact, Microsoft dropped it out of WPF and will hardly support it. More importantly, you will scare off all your users if you use MDI. Just don't. I can explain what to do instead.

Please see:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages[^],
Question on using MDI windows in WPF[^],
MDIContainer giving error[^],
How to Create MDI Parent Window in WPF?[^].

—SA
 
Share this answer
 
Comments
VJ Reddy 13-Jun-12 13:07pm    
Good suggestion and references. 5!
Sergey Alexandrovich Kryukov 13-Jun-12 18:25pm    
Thank you, VJ.
--SA
Espen Harlinn 14-Jun-12 18:43pm    
Nice reply :-D
Sergey Alexandrovich Kryukov 14-Jun-12 18:46pm    
Thank you, Espen.
--SA
C#
// Assume that your parent window is parentForm
// When you are creating each form, add it to parentForm's owned forms
// Let's say you're creating form1
MyForm form1 = new MyForm();
parentForm.AddOwnedForm(form1);//if you're in your parentForm's class use this.AddOwnedForm(form1);
form1.Show();

// then when you want to close all of them simple call the below code

foreach(Form form in parentForm.OwnedForms)
{
      form.Close();
}

// And also you can call this if you're in parentForm's class
foreach(Form form in this.OwnedForms)
{
      form.Close();
}</pre>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900