简体   繁体   English

如何在 MDI 主父级中调用方法或函数我需要在类上实例化一个表单,新实例父级应该是 mainparent 吗?

[英]how to call a method or function inside MDI main parent I need to instantiate a form on class and new instance parent shoud mainparent?

//MdiParent  mainparent.cs

        public static void lodforweb()
        {

            frm_webcs frmload_webcs = new frm_web
            {
                MdiParent = this
            };

            frmload_webcs.Show();

        }

//Context menu class  

//Cl_contextmenu.cs


    public bool OnContextMenuCommand()
        {
            if (commandId == (2020)
            {
             mainparent.lodforweb();
                return true;
            }

        }
}

// having a problem with "this" using static method // instantiating does not work also. // 使用静态方法时“this”有问题 // 实例化也不起作用。

From the comments:来自评论:

once I right click the child form context menu popups.一旦我右键单击子表单上下文菜单弹出窗口。 I want to generate another child form and should be child of mainparent我想生成另一个子表单,应该是 mainparent 的孩子

Also provided from the comments:还从评论中提供:

private void childform_Load(object sender, EventArgs e) {
    cl_chromebrowser urload = new cl_chromebrowser(); 
    panel1.Controls.Add(urload.choniumeload("www.site.com")); 
    urload.chromebrowser.MenuHandler= new cl_contexmenu();
}

So your child form is called "childform".所以你的子表单被称为“childform”。 Add a static member that holds a reference to the MDI parent, and set it in the Load() event:添加一个静态成员来保存对 MDI 父级的引用,并在 Load() 事件中设置它:

public static Form MyMdiParent;

private void childform_Load(object sender, EventArgs e)
{
    // ... your existing code from above ...

    childform.MyMdiParent = this.MdiParent;
}

Now you can use that MDI parent directly from your context menu:现在,您可以直接从上下文菜单中使用该 MDI 父项:

public bool OnContextMenuCommand()
{
    if (commandId == 2020)
    {
        if (childform.MyMdiParent != null)
        {
            childform.MyMdiParent.Invoke((MethodInvoker)delegate ()
            {
                frm_webcs frmload_webcs = new frm_webcs();
                frmload_webcs.MdiParent = childform.MyMdiParent;
                frmload_webcs.Show();
            });
        }
    }
    return true;
}

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

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