简体   繁体   中英

Show MDIChild Form from another MDIChild Form

Let's I have MDIParentForm (Home) , Child Form (Form1, Form2) , Home has one Menu (Add) and Form1 has button (btnOk)

'''''Here is C# Code''''''

 public partial class Home : DevExpress.XtraBars.Ribbon.RibbonForm
{
    public Home()
    {
        InitializeComponent();
    }
    public void  CreateForm(Form frm)
    {
        frm.MdiParent = this;
        frm.WindowState = FormWindowState.Maximized;
        frm.Show();
    }
    private void btnAddForm_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
        Form1 frm = new Form1();
        CreateForm(frm);
    }
}
//Up to this 1st level my code works fine.
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void btnDone_Click(object sender, EventArgs e)
    {
//From Here I am not able show the Form2.
        Home MDIParentForm = new Home();
        Form2 frm = new Form2();
        MDIParentForm.CreateForm(childForm);
    }
}

Action: On the click of Add Menu I used to open Form1 (It works fine) and on the click of btnOk I want to open Form2 (which is another MDIChild form).

Let me know how we can accomplish this task.

What you do is you create yet another instance of your MDI parent and you set the Form2 as a child of this newly created MDI parent. This is clearly wrong, you want both to be children of the very same parent.

Either do this directly:

private void btnDone_Click(object sender, EventArgs e)
{
    Form2 frm = new Form2();
    // both have the same MDI parent
    frm.MDIParent = this.MDIParent;
    frm.Show();
}

or, if you insist on reusing your CreateForm

private void btnDone_Click(object sender, EventArgs e)
{
    Form2 frm = new Form2();
    ((Home)this.MDIParent).CreateForm( frm );
}

Both approaches depend on this.MDIParent set in form1 and make sure the same instance is used for newly created form2.

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