Well, if I understand you right, you want to call forms and put them as children of Mdiparent Formcaller. I did something similar, but before, let’s put some stitches...
The way you’re suggesting: caller.CallForm(anotherForm)
, requires the class Formcaller "know" all the forms that will be invoked. See if I can help you extract some idea with the implementation below...
To solve the problem, I had created a class/enumeration just to describe the MDI child forms, but maintenance was very constant. I then decided to change strategy and create a pattern that could be reused by any other Mdiparent.
- I created an interface that forces all Mdiparent know how to invoke your children (in my case, by index);
- I implemented the interface in an abstract class that would execute the desired patterns (in this case, invoke forms);
- Inherit the abstract class in any target Mdiparent.
Something like:
public interface IMdiContainer
{
Form GetCurrentForm(int currentStep);
}
public abstract class MdiContainerBase
{
private IMdiContainer _container;
protected int index;
public MdiContainerBase(IMdiContainer container)
{
_container = container;
}
protected override void OnShow(EventArgs e)
{
base.OnShow(e);
// Call mdiChild from mdiContainer
ShowMdiChild(_container.GetCurrentForm(index));
}
protected override void NextForm()
{
ShowMdiChild(_container.GetCurrentForm(++index));
}
protected override void PrevForm()
{
ShowMdiChild(_container.GetCurrentForm(--index));
}
private void ShowMdiChild(Form form)
{
form.MdiParent = this;
form.Show();
}
// and so on...
}
public class MyCustomMdiContainer : MdiContainerBase, IMdiContainer
{
public MyCustomMdiContainer() : base(this)
{
}
// implementation forced by interface
public Form GetForm(int index)
{
switch(index)
{
case 1:
return new Form1();
case 2:
return new Form2();
default:
return null;
}
}
}
So you can create everything you need in the interface... Just need to create the switch... I hope I’ve helped in some way :)