How can I access all application forms?

D

dusiapapa

Hi everybody!

I have windows forms application containing a lot of MDI (shown with
..Show()) and modal (shown with .ShowDialog()) forms.
At some instant I need to kill all of them exept main application
window.
MDI forms can be accessed using MDIChildren property of the main form.
But how can I access modal dialogs?
May be there is better way to implement this task?

Thanks in advance!
 
M

Michael C

dusiapapa said:
Hi everybody!

I have windows forms application containing a lot of MDI (shown with
.Show()) and modal (shown with .ShowDialog()) forms.
At some instant I need to kill all of them exept main application
window.
MDI forms can be accessed using MDIChildren property of the main form.
But how can I access modal dialogs?
May be there is better way to implement this task?

Thanks in advance!

There is no built in way, you need to keep a list of modal windows in an
arraylist or something.
 
B

Bojan Mijuskovic

If the forms that you want to close are modal (i.e. you used ShowDialog()
method) try iterating through OpenForms collection of the Application class.
Something like this:

foreach (Form f in Application.OpenForms)
{
if (f != this) // don't consider current
// form, let's say the main form
{
try
{
// set modal result, probably Cancel is the neutral one
f.DialogResult = DialogResult.Cancel;

// this should schedule the form instance to be
//collected by GC, although not necessary
f.Dispose();
}
catch
{
// in case something goes wrong
// depending on your logic try handling this
}
}
}

Hope this gives you hints on how to apply it to your problem.


Regards,
Bojan Mijuskovic [MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top