简体   繁体   English

如何从MDI父级C#调用子窗体的方法

[英]How do I call a method of a child form from mdi parent c#

I have several child forms but they have one common methods in them, get_CurrentClamp() . 我有几种子窗体,但是它们中有一种通用方法get_CurrentClamp() i want to call the method of the current active mdichild from the MDI parent. 我想从MDI父级调用当前活动的mdichild的方法。

This is the onclick event of a menuitem in the MDIparent form MDIMain.cs that should call the method. 这是MDIparent形式的MDIMain.cs菜单项的onclick事件,应调用该方法。

....
 private void mnugetCToolStripMenuItem_Click(object sender, EventArgs e)
   {
    if (MdiChildren.Any())
            {
                Form f = this.ActiveMdiChild;            
               f.get_CurrentClamp(varCurrentThreshhold);
            }
   }
.....

In the child form frmDashboard.cs 在子窗体中frmDashboard.cs

public void get_CurrentClamp(float curThreshhold=5.5)
        {
           ...
        }

But i keep getting an error, any where am going wrong? 但是我总是出错,哪里出了问题? Any help will be greatly appreciated! 任何帮助将不胜感激!

the error a getting is this 错误是这个

Error 3 'System.Windows.Forms.Form' does not contain a definition for 'get_CurrentClamp' and no extension method 'get_CurrentClamp' accepting a first argument of type 'System.Windows.Forms.Form' could be found (are you missing a using directive or an assembly reference?) 错误3'System.Windows.Forms.Form'不包含'get_CurrentClamp'的定义,并且找不到扩展方法'get_CurrentClamp'接受类型为'System.Windows.Forms.Form'的第一个参数(您是否缺少使用指令还是程序集引用?)

Thats the error am getting on the mdiparent form. 多数民众赞成在错误越来越中间形式。

If you're sure the active form will be one of the frmDashboard instances, you can declare f to be of that type: 如果您确定活动表单将是frmDashboard实例之一,则可以将f声明为该类型:

frmDashboard f = this.ActiveMdiChild;

You might want a try/catch around this just in case. 您可能需要尝试一下以防万一。 (Works in VB, anyway. Not sure about C#.) (无论如何,都可以在VB中使用。不确定C#。)

Thanks to Idle_Mind i solved the problem by using an Interface. 多亏了Idle_Mind,我通过使用接口解决了这个问题。 i created a new interface in a file called IChildMethods.cs and below is the interface 我在名为IChildMethods.cs的文件中创建了一个新接口,下面是该接口

 internal interface IChildMethods
    {
        void get_CurrentClamp(float curThreshhold=5.5);
    }

and on the child forms i just included the interface like below on the form frmDashboard.cs ; 在子窗体上,我只是在frmDashboard.cs窗体上包括如下所示的接口;

 public partial class frmDashboard : Form, IChildMethods

and on the mdiform MDIMain.cs 并在mdiform MDIMain.cs上

....
 private void mnugetCToolStripMenuItem_Click(object sender, EventArgs e)
   {
    if (MdiChildren.Any())
            {
               if (this.ActiveMdiChild is IChildMethods)
            {
                ((IChildMethods)this.ActiveMdiChild).get_CurrentClamp(varCurrentThreshhold);
            }            

            }
   }
.....

I havent tried using the Reflection method since the Interface method worked, but am just wondering if Reflection is better than using an Interface in such a problem 自从接口方法起作用以来,我还没有尝试过使用反射方法,但是我只是想知道在这种问题上反射是否比使用接口更好

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

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