MDI children instances, how to code
-
Hi, I have trouble trying to find out which instance of a MDI child is active and how to adress it's properties and methods. I have got a MDI form with child frmList that shows all the available info in a particular case, then clicking on one of the nodes should pop up an frmShow that displays the info in a grid on a background image. Adding to this is a from frmProperties with certain tools that should depict what the user can do on the frmShow window (Be it zooming in, adding lines etc). The problem is that there must allways just be one instance of frmList and frmProperties but multiple instances of frmShow. I think I have it sorted with: (make suggestions please !)
bool isOpen = false; // loop through list and find if child is open // for (int x = 0; x < this.MdiChildren.Length; x++) { if (this.MdiChildren\[x\].Name == "frmListDef") { isOpen = true; } } // if it does not exist open it if (!isOpen) { Form frmList = new frmListDef(); frmList.MdiParent = this; frmList.Show(); }
Now I am trying to find out how to adress a method or property on the specific instance of a child ? thanks Ian PS: any nice articles or refs to MDI programming would be nice
-
Hi, I have trouble trying to find out which instance of a MDI child is active and how to adress it's properties and methods. I have got a MDI form with child frmList that shows all the available info in a particular case, then clicking on one of the nodes should pop up an frmShow that displays the info in a grid on a background image. Adding to this is a from frmProperties with certain tools that should depict what the user can do on the frmShow window (Be it zooming in, adding lines etc). The problem is that there must allways just be one instance of frmList and frmProperties but multiple instances of frmShow. I think I have it sorted with: (make suggestions please !)
bool isOpen = false; // loop through list and find if child is open // for (int x = 0; x < this.MdiChildren.Length; x++) { if (this.MdiChildren\[x\].Name == "frmListDef") { isOpen = true; } } // if it does not exist open it if (!isOpen) { Form frmList = new frmListDef(); frmList.MdiParent = this; frmList.Show(); }
Now I am trying to find out how to adress a method or property on the specific instance of a child ? thanks Ian PS: any nice articles or refs to MDI programming would be nice
-
Hi, I have trouble trying to find out which instance of a MDI child is active and how to adress it's properties and methods. I have got a MDI form with child frmList that shows all the available info in a particular case, then clicking on one of the nodes should pop up an frmShow that displays the info in a grid on a background image. Adding to this is a from frmProperties with certain tools that should depict what the user can do on the frmShow window (Be it zooming in, adding lines etc). The problem is that there must allways just be one instance of frmList and frmProperties but multiple instances of frmShow. I think I have it sorted with: (make suggestions please !)
bool isOpen = false; // loop through list and find if child is open // for (int x = 0; x < this.MdiChildren.Length; x++) { if (this.MdiChildren\[x\].Name == "frmListDef") { isOpen = true; } } // if it does not exist open it if (!isOpen) { Form frmList = new frmListDef(); frmList.MdiParent = this; frmList.Show(); }
Now I am trying to find out how to adress a method or property on the specific instance of a child ? thanks Ian PS: any nice articles or refs to MDI programming would be nice
The problem with this approach is the MdiChildren property of the parent is an array of forms so you can only see the standard properties and methods of the Form class unless you cast each member of the array to the appropriate class. That can be done by the code below. Assuming you have a class ChildForm1 : Form with a method DoSomething and property SomeProperty.
foreach (Form child in this.MdiChildren)
{
if (child is ChildForm1)
{
ChildForm1 theChild = (ChildForm1)child;
MessageBox.Show(theChild.SomeProperty);
theChild.DoSomething();
}
}This is going to get messy if you have many forms with many properties and methods and will difficult to maintain. You could use reflection to get all the info about the child forms and their methods/properties but that may be a little slow depending on just how much reflection you need to do, but is probably the best solution here. Have you thought about creating an interface that your all child forms can implement as well as deriving form Form? You could add all the properties and methods that will be available in the children to the interface and implement them on the appropriate forms. The ones that shouldn't implement a specific property or method could throw a NotImplementedException so you know it's not available.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog) -
The problem with this approach is the MdiChildren property of the parent is an array of forms so you can only see the standard properties and methods of the Form class unless you cast each member of the array to the appropriate class. That can be done by the code below. Assuming you have a class ChildForm1 : Form with a method DoSomething and property SomeProperty.
foreach (Form child in this.MdiChildren)
{
if (child is ChildForm1)
{
ChildForm1 theChild = (ChildForm1)child;
MessageBox.Show(theChild.SomeProperty);
theChild.DoSomething();
}
}This is going to get messy if you have many forms with many properties and methods and will difficult to maintain. You could use reflection to get all the info about the child forms and their methods/properties but that may be a little slow depending on just how much reflection you need to do, but is probably the best solution here. Have you thought about creating an interface that your all child forms can implement as well as deriving form Form? You could add all the properties and methods that will be available in the children to the interface and implement them on the appropriate forms. The ones that shouldn't implement a specific property or method could throw a NotImplementedException so you know it's not available.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog) -
Thanks Dave, It works !! Do you know of a good reference on more MDI topics ? A step by step guide to get abit more out of my MDI code ! cheers Ian
Not really - MDI is much the same as any other winforms app. The only suggetions I would give are: 1. Get very familiar with events and delegates as inevitably you'll find the need to get your child forms communicating with their parent and siblings. 2. All your child forms are actually hosted in a container which is an instance of the MdiClient class. You may find sometimes it's useful to have access to that control. Creating this readonly property and member variable in your MdiParent will make this much easier.
private MdiClient m\_MdiClient; public MdiClient MdiClient { get { if (m\_MdiClient != null) return m\_MdiClient; foreach (Control control in Controls) { if (control is MdiClient) { m\_MdiClient = (MdiClient)control; return m\_MdiClient; } } throw new Exception("MdiClient control could not be found"); } }
You then have access to all the containers properties, methods and events!
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog)