简体   繁体   English

从父级调用特定 MDI 子级的方法

[英]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 .我有一个 MDI 父窗口,它可以包含来自特定子项的多个实例,称为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现在,当从父级单击特定控件时,我需要获取活动的 frmChild 并从 frmChild 调用特定方法

Below is an image of what I am trying to achieve (get active MDI child and invoke a particular method from that class):下面是我试图实现的图像(获取活动的 MDI 子项并从该类调用特定方法):

一只忙碌的猫

Now each frmChild is instantiated by:现在每个 frmChild 被实例化:

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:当我想从活动的 frmChild调用一个方法时,我正在尝试以下操作并被卡住:

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.我仍在学习 OOP,我确信这是一些原则。 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: ** **注意:我读到干扰 frmChild 是解决此问题的最佳方法,因此主窗体不需要在每个 frmChild 中进行挖掘,因此我创建了 frmChild 使用的接口,即:**

 public interface NewFileFormInterface
{
    void saveFile();
}

Now, saveFile() is the method I want to invoke from Main, which is implemented in frmChild.现在, saveFile()是我想从 Main 调用的方法,它在 frmChild 中实现。

Any help on this issue and some keywords I can research would be great.关于这个问题的任何帮助以及我可以研究的一些关键字都会很棒。

TIA!蒂亚!

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

The as keyword performs a run-time cast. as 关键字执行运行时强制转换。 If the object (ActiveMdiChild in this case) is null or is not of the as type, the result will be null.如果对象(在本例中为 ActiveMdiChild)为 null 或不是 as 类型,则结果将为 null。

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

You might also be interested in reading: .Net Naming Convention Guidelines您可能还有兴趣阅读: .Net 命名约定指南

try this:试试这个:

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

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.希望这可以帮助其他正在寻找更简洁有效的答案的人。

I did it this way.我是这样做的。 "AuditAddMod" is a form. “AuditAddMod”是一种形式。 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.该表单中有一个公共的“PassData()”方法,该方法被调用并在同级表单中执行我需要的操作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM