C# 父子关系中的 MDI 表单

发布于 2024-11-08 18:17:02 字数 488 浏览 4 评论 0原文

我的项目有 3 个表格。

  • form1 是 MDI 控制器
  • form2form3 是 MDI 子级

如何创建 form1 作为父级和 form2form3 作为子项?

类似于旧 MFC 的 MDI 界面:

在此处输入图像描述

假设 form2 是父级并且有一个按钮。如果单击,则必须在父表单 (form1) 中打开 form3。我该如何设置?

I have 3 forms on my project.

  • form1 is MDI controller
  • form2 and form3 are MDI children

How do I create form1 as the parent and form2 and form3 as children?

Something like old MFC's MDI interface:

enter image description here

Imagine form2 is the parent and has a button. If clicked, it must open form3 in the parent form (form1). How do I set this up?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏末染殇 2024-11-15 18:17:02

首先,确保 Form1 的 IsMdiContainer 设置为 true

然后实例化 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;

在 Form2 的代码中,使用类似以下内容来处理按钮的单击事件以实例化 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;
        }
    }
}

需要注意的是,这段代码确实又快又脏。它有一些不受欢迎的事情,例如 Form2 和 Form3 的耦合(以及无用的类名 Form1、Form2 和 Form3)。理想情况下,您可以通过从 Form2 引发一个事件来解耦 Form2 和 Form3,您的表单容器可以挂接到该事件并实例化 Form3。此示例代码旨在为您提供指导。

Firstly, make sure Form1's IsMdiContainer is set to true.

Then instantiate Form1 and Form2, setting Form1 to be Form2's 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.

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). Ideally, you'd decouple Form2 and Form3 by raising an event from Form2 that your forms container can hook onto and instantiate Form3. This sample code is meant to give you a direction.

假扮的天使 2024-11-15 18:17:02

只需告诉表单其 MdiParent 是当前表单即可。

  form2 frm = new form2 ();
    frm.MdiParent = this;
    frm.Show();

Just tell to the form that its MdiParent is current form.

  form2 frm = new form2 ();
    frm.MdiParent = this;
    frm.Show();
记忆里有你的影子 2024-11-15 18:17:02
    private void homeToolStripMenuItem_Click(object sender, EventArgs e)
    {
        frmHome objfrmHome = frmHome.GetChildInstance();
        objfrmHome.MdiParent = this;
        objfrmHome.WindowState = FormWindowState.Maximized;
        objfrmHome.Show();
        objfrmHome.BringToFront();
    }

然后在您调用的表单中

    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

    }
    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

    }
青巷忧颜 2024-11-15 18:17:02

试试这个功能

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();

    }

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();

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文