Close all the child forms of a form

Hello experts,

I am creating several child forms from a main-form in my code. And then there is a sign-in/ sign-out logic. I want all my child forms to be closed at the time of sign-out, while keeping the main-from open for another log-in session.

how do I accomplish this. This is a winform application in C#.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

//*** 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);

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

foreach(Form form in parentForm.OwnedForms)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
//***And also you can call this if you're in parentForm's class
foreach(Form form in this.OwnedForms)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.