Iterating through forms in an application

J

June Homer

I want to be able to reference an open form in my application. I
thought this would be easy by iterating through all forms in the
application but I can't figure out how to do this. I have tried using
the Form.mdiChildren as recommended in another post but this did not
return any results. This does not surprise me as all forms in the
application are forms in their own right.

Any help appreciated...
 
C

Carsten Posingies

June said:
I want to be able to reference an open form in my application. I
thought this would be easy by iterating through all forms in the
application but I can't figure out how to do this. I have tried using
the Form.mdiChildren as recommended in another post but this did not
return any results. This does not surprise me as all forms in the
application are forms in their own right.

Any help appreciated...

Hi June,

did you set the forms' MdiParent property?
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (June Homer) scripsit:
I want to be able to reference an open form in my application. I
thought this would be easy by iterating through all forms in the
application but I can't figure out how to do this. I have tried using
the Form.mdiChildren as recommended in another post but this did not
return any results. This does not surprise me as all forms in the
application are forms in their own right.

There is no support for doing that included in the .NET Framework. You
may want to maintain a separate arraylist/collection that stores the
references to the form instances.
 
J

June Homer

Hi Carsten

Since my original post I have realised that I would need to set the
mdiParent property of each new form.

However, I do not want to have to do this as all forms are different. Is
there not a simple way to iterate through all open form in an
application?

Regards
June
 
J

JezB

This is what I do. In all my forms I instantiate a FormConfig class from the
Load event, passing it the form reference. So in here I can do anything I
like to affect all my forms - maintain a collection of all open forms,
attach handlers for specific form events (eg. KeyDown to assign and handle
global hotkeys, Closed to remove the form from the collection when it's
closed) etc, all in one central place.
 
C

Carsten Posingies

June said:
Hi Carsten

Since my original post I have realised that I would need to set the
mdiParent property of each new form.

However, I do not want to have to do this as all forms are different.
Is there not a simple way to iterate through all open form in an
application?

I don't get this point ("all forms are different"), since all forms are
descendants of System.Windows.Forms.Form, but whatsoever, how about
this:

Pass a reference to your main app object (the MDI window, the "main"
window, an instance of an app class, a static app class or the like) to
your forms when creating them. After the creation stuff has been
completed for the specific form, let the form call a method of your app
object (sth. like "App.RegisterForm(this);"). Let this method put the
reference to the "child" form into a hash table with key and value as
the reference. Now you can conveniently foreach(...) over this hash
table and do whatever you want to.

Of course, before destroying the form, i.e. somewhere in the close
event, the form has to unregister from the app object (again, sth.
similar to "App.UnregisterForm(this);"). Those register and unregister
methods can, for an MDI app, even adjust the "Window" menu's windows
list and stuff like this.

In my own multi-window apps I do this quite similar, except that, since
I'm a "don't mix data and views", I've created my own collection for
child windows that extends this stuff in terms of dealing with the data
the form is displaying (like storing it, or asking the user if she wants
to trashcan the changes she made etc.).

IMHO it is always a good idea to see a form as a "view" and not to
implement any program/business logic in a form's code, but to be
n-tiered even in the client. But this is really MHO. <g>

Carsten
 

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