How can I access all application forms?

  • Thread starter Thread starter dusiapapa
  • Start date Start date
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!
 
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.
 
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]
 
Back
Top