Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MDI Parent calling Child's functions. 1

Status
Not open for further replies.

DynamoCL

Programmer
Mar 24, 2006
5
GB
Hi all.

I am developing an MDI application, but have hit a snag. I have multiple different MDI child forms, that may be present in the MDI parent. Each of the child forms (not the same form) have a SaveData function.

I have a button on the MDI parent. I want this button to run the active child's SaveData function. I have tried to use inheritence on the forms, so that the SaveData function is always present (overridden by each instance). But I still cant get this to work as it should.

Here is what I have (only pasted relevant code):

Here is the form I inherit for the child forms:


public partial class frmTemplate : Form
{
public void SaveData()
{
MessageBox.Show("Function Not Implemented");
}
}


Example of one of the child form:

public partial class frmTest : frmTemplate
{
public void SaveData()
{
MessageBox.Show("Save Clicked " + strFormName );
//At the moment just show text until it works
//This should overide the template function
}
}

Now in the MDI parent (have shown 2 examples to show my trouble):

private void Save()
{
//First Test - IT WORKS... however have to know what frm
frmTest frmTemp = (frmTest)this.ActiveMdiChild;
frmTemp.SaveData();

//I guess this does what it should, but not what I want.
frmTemplate frmTemp2 = (frmTemplate)(this.get);
frmTemp2.SaveData();
}

When I cast the activeMDIChild to the correct form that is the active child, it works, I get a message box with the function overide text. This is OK. However, the problem is I am going to have at least 30 different forms, and hence will need to write out the static cast for each case... this I would like to avoid (feels sloppy):

if (Active form is of type x)
Cast for x type
if (Active form is of type y)
Cast for y type
if (active form is of type z)
Cast for z type
..... and so on.... sloppy.


The second call (casting to the form that is inherited), this just outputs the standard not implemented text. I guess this is as it should. However, I thought that since this function was overidden, it would have called the correct function.

Is there a solution to this problem here? Or is there a better way to acheive what I am after.

Thanks for any help.






 
Hi,

I think you can best use an interface to solve your problem.

Possible solution:
- Create an interface:
Code:
using System;

namespace MDI_Issue
{
    public interface [b][COLOR=blue]IChildFormInterface[/color blue][/b]
    {
        void SaveData();
    }
}

- Create your childform and let them inherit the interface:
Code:
namespace MDI_Issue
{
    public partial class ChildForm1 : Form, [b][COLOR=blue]IChildFormInterface[/color blue][/b]
    {
        public ChildForm1()
        {
            InitializeComponent();
        }

        public void SaveData()
        {
            MessageBox.Show("ChildForm1 SaveData()");
        }
    }
}

- Create the parent. When executing the SaveData function, you need to convert the activechildform to the created interface and call the interface 's SaveData()
Code:
private void saveDataToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (this.ActiveMdiChild != null)
    {
        [b][COLOR=blue]IChildFormInterface childForm = (IChildFormInterface)this.ActiveMdiChild;[/color blue][/b]
        [b][COLOR=blue]childForm[/color blue][/b].SaveData();
    }
}

private void Form1_Load(object sender, EventArgs e)
{
    ChildForm1 child1 = new ChildForm1();
    child1.MdiParent = this;
    child1.Show();

    ChildForm2 child2 = new ChildForm2();
    child2.MdiParent = this;
    child2.Show();
}

Greetz,

Geert
 
Thats it!!!!!

Thanks so much for the help, it worked. I have tried so many ways to get this to work (mostly old C++ ways), you are a star. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top