简体   繁体   English

C#父母与子女中的MDI表单

[英]MDI form in C# Parent And Child

I have 3 forms on my project. 我的项目有3个表格。

  • form1 is MDI controller form1是MDI控制器
  • form2 and form3 are MDI children form2form3是MDI子

How do I create form1 as the parent and form2 and form3 as children? 如何创建form1作为父级,并创建form2form3作为子级?

Something like old MFC's MDI interface: 类似于旧的MFC的MDI接口:

在此处输入图片说明

Imagine form2 is the parent and has a button. 想象一下form2是父级并且有一个按钮。 If clicked, it must open form3 in the parent form ( form1 ). 如果单击,则必须在父窗体( form1 )中打开form3 How do I set this up? 我该如何设置?

Firstly, make sure Form1's IsMdiContainer is set to true . 首先,确保Form1的IsMdiContainer设置为true

Then instantiate Form1 and Form2, setting Form1 to be Form2's MdiParent: 然后实例化Form1和Form2,将Form1设置为Form2的MdiParent:

// Form1.IsMdiContainer should be true
Form1 form1 = new Form1();

// This automatically adds form2 into form1's MdiChildren collection
Form2 form2 = new Form2();
form2.MdiParent = form1;

In Form2's code, have something like the following to handle the button's click event to instantiate Form3. 在Form2的代码中,具有类似以下内容的内容,以处理按钮的click事件以实例化Form3。

public class Form2 : Form {
    // Include as data member so we only instantiate one Form3
    Form3 _form3;

    public Form2() {
        InitializeComponent();
        Button1.Click += new EventHandler(Button1_Click);
    }

    private void Button1_Click(object sender, EventArgs e) {
        if(_form3 == null) {
            _form3 = new Form3();
            // Set Form3's parent to be Form1
            _form3.MdiParent = this.MdiParent;
        }
    }
}

As a couple notes, this code is really quick and dirty. 几点注意,这段代码确实快速又肮脏。 There are several undesirable things about it like the coupling of Form2 and Form3 (as well as unhelpful class names Form1, Form2, and Form3). 关于它,有一些不良的事情,例如Form2和Form3的耦合(以及无用的类名Form1,Form2和Form3)。 Ideally, you'd decouple Form2 and Form3 by raising an event from Form2 that your forms container can hook onto and instantiate Form3. 理想情况下,您可以通过引发Form2可以挂接到并实例化Form3的事件来使Form2和Form3脱钩。 This sample code is meant to give you a direction. 此示例代码旨在为您提供指导。

Just tell to the form that its MdiParent is current form. 只需告诉表单其MdiParent是当前表单即可。

  form2 frm = new form2 ();
    frm.MdiParent = this;
    frm.Show();
    private void homeToolStripMenuItem_Click(object sender, EventArgs e)
    {
        frmHome objfrmHome = frmHome.GetChildInstance();
        objfrmHome.MdiParent = this;
        objfrmHome.WindowState = FormWindowState.Maximized;
        objfrmHome.Show();
        objfrmHome.BringToFront();
    }

Then in the Form you are calling 然后在表格中调用

    private static frmHome m_SChildform;
    public static frmHome GetChildInstance()
    {
        if (m_SChildform == null) //if not created yet, Create an instance

            m_SChildform = new frmHome();
        return m_SChildform;  //just created or created earlier.Return it

    }

try this function 试试这个功能

public void mdiChild(Form mdiParent, Form mdiChild)
    {
        foreach (Form frm in mdiParent.MdiChildren)
        {
            if (frm.Name == mdiChild.Name)
            {

                frm.Focus();
                return;
            }
        }

        mdiChild.MdiParent = mdiParent;
        mdiChild.Show();

    }

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

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