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