简体   繁体   中英

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.

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

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". Add a static member that holds a reference to the MDI parent, and set it in the Load() event:

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:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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