finding all the opened forms

  • Thread starter Thread starter Jia
  • Start date Start date
J

Jia

I have an application that has a main form and a user can create a number of
other forms at run time. Most of those forms are created through reflection
by first using Assembly.CreateInstance to create the object then get the
methodInfo of a certain method, and finally calls MethodInfo.Invoke.

At certain point of the execution I need to close all those forms except my
main form.
Anybody knows if there is a way of finding all those forms from my main
form?
Thanks.
 
Hi Jia, Joseph's suggestion would work fine.
Additionally I have another method which I prefer and that is to subscribe
each form's close method to a
particular delegate.
Additionally within each forms closed event handler remove them from this
delegate (so they won't be fired again).
Therefore the delegate list will only fire any forms close methods if they
are open.
It is simple, and works a treat.

Br,

Mark.
 
Hi Jia

One more idea for you - you can set the Owner property of the new forms to
the main form in the application. Then the main form can use its OwnedForms
property to iterate through all these forms.

The downside of this is that the OwnedForms always float on top of the Owner
(it may not be a downisde for you of course) - if you don't want this, then
you will need a mechanism like the one the other posters have outlined.

HTH

Nigel
 
Back
Top