0
votes

IDE: Microsoft Visual Studio 2010 Platform : C#.net

I have Child forms being loaded on the MDI parent form. So, now i want to close the MDI parent and all its child form when I click on the finish button which is in the last child form. And once i click on the finish button it should close all the instances of parent and child form and open another form which is not part of the MDI parent form ?

1

1 Answers

0
votes

You could try to enumerate forms in Application.OpenForms, check their relations and close what you need:

// close childs of parentForm
foreach(var form in Application.OpenForms)
    if(form.MDIParent == parentForm)
        form.Close();

Or you can register childs when they are created, so you will have a list of childs to close (pseudo-code):

// container for childs
List<Form> _childs = new List<Form>();
// when adding new form
Form1 form1 = new Form1();
form1.MDIParent = parentForm;
// register it
_childs.Add(form1);
// unregister when child is closed
form.OnClosed += ChildClosed;

// in ChildClosed
// unregiser
_childs.Remove(sender as Form);

It's just a robust idea. Childs registering could be optimized (by having base class for childs which does all that automatically). Registering childs is better if you going to have some extra data for each childs, to example, store they location and size for layouting.