brazc0re
brazc0re

Reputation: 353

Call method of specific MDI child from parent

I have a MDI parent window that can contain mulitple instances of a particular child from, call it frmChild. Now when a particular control is clicked from the parent, I need to get the active frmChild and invoke a particular method from frmChild

Below is an image of what I am trying to achieve (get active MDI child and invoke a particular method from that class):

a busy cat

Now each frmChild is instantiated by:

private void newFileToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        frmNewDocument = new frmNewDocument();
        frmNewDocument.MdiParent = this;
        frmNewDocument.Show();
    }

When I want to invoke a method from the active frmChild, I am trying the following and am stuck:

private void saveFileToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        /* get active MDI child*/
        Form frmActiveNewDocument = this.ActiveMdiChild;           
        
        /* make sure MDI child is a "New Document" type form */
        if (frmActiveNewDocument.GetType() == frmNewDocument.GetType())
        {
            /* invoke a method from active frmChild here */
        }
    }

I am still learning OOP and am sure this is some principle. If that can be mentioned in the article, that would be great also.

**Note: I read that an interference for frmChild is the best way to approach this issue so the Main form doesn't need to go digging around in each frmChild, so I have created an interface that frmChild uses, which is: **

 public interface NewFileFormInterface
{
    void saveFile();
}

Now, saveFile() is the method I want to invoke from Main, which is implemented in frmChild.

Any help on this issue and some keywords I can research would be great.

TIA!

Upvotes: 3

Views: 3898

Answers (4)

CW1255
CW1255

Reputation: 121

I did it this way. "AuditAddMod" is a form. I have parallel children. There is a public "PassData()" method in that form that is called and does what I need in the sibling form.

private void LoadRelatedFollowUpAuditTable()
{
   foreach (Form form in this.MdiParent.MdiChildren)
   {
      if (form.Name == "AuditAddMod" && form.Text != this.Text)
      {
         ((AuditAddMod)form).PassData(form.Name, 1);
      }
   }
}

Upvotes: 0

Joel Trauger
Joel Trauger

Reputation: 732

I know this is old, but this has always worked for me.

In the Child code:

public void SaveFile()
{
  // Some code goes here.
}

And in the Parent code:

frmNewDocument child = this.ActiveMdiChild as frmNewDocument;
if(child != null)
{
  child.SaveFile();
}

Hope this helps someone else who is looking for a more concise answer that works.

Upvotes: 1

Adam
Adam

Reputation: 26917

try this:

if (ActiveMdiChild is frmNewDocument)
{
    (ActiveMdiChild as frmNewDocument).saveFile();
}

Upvotes: 1

Tergiver
Tergiver

Reputation: 14517

frmNewDocument child = ActiveMdiChild as frmNewDocument;
if (child != null)
{
    child->saveFile();
}

The as keyword performs a run-time cast. If the object (ActiveMdiChild in this case) is null or is not of the as type, the result will be null.

http://msdn.microsoft.com/en-us/library/cscsdfbt.aspx

You might also be interested in reading: .Net Naming Convention Guidelines

Upvotes: 2

Related Questions