简体   繁体   English

C# 带有拆分容器的 Winform MDI 表单 - MdiChildren 为空

[英]C# Winform MDI Form with Split Container - MdiChildren is empty

I am creating a C#.Net 4.8 Winform app with one MDI Form.我正在创建一个带有一个 MDI 表单的 C#.Net 4.8 Winform 应用程序。 This MDI Form has several panels and a split container.这个 MDI 表单有几个面板和一个拆分容器。

When adding a childform I use the following code:添加子窗体时,我使用以下代码:

frm.Text = formTitleText;
frm.MdiParent = this;
frm.TopLevel = false;
frm.FormBorderStyle = FormBorderStyle.None;
frm.Dock = DockStyle.Fill;
panelDeskTop.Controls.Add(frm);
panelDeskTop.Tag = frm;
frm.BringToFront();
lblTitleChildForm.Text = $"{frm.Text}({this.MdiChildren.Length})";
frm.Show();

The form shows in the panel, but is not added as a MDI Child.窗体显示在面板中,但未添加为 MDI 子项。 The Parent (This) is the MDI Parent and has property of IsMDIContainer. Parent (This) 是 MDI Parent 并且具有 IsMDIContainer 的属性。 So the this.MdiChildren.Length is always 0.所以 this.MdiChildren.Length 始终为 0。

Not sure why?不确定为什么?

Thanks in advance.提前致谢。

It's expected.这是预期的。 You've set another parent for it: panelDeskTop.Controls.Add(frm);你已经为它设置了另一个父级: panelDeskTop.Controls.Add(frm); . .

Consider the following points:考虑以下几点:

  • An MDI parent form has a control of type MdiClient . MDI 父窗体具有类型为MdiClient的控件。 When you set MdiParent of a form, basically you are adding it to the controls collection of the MdiClient control.当您设置窗体的MdiParent时,基本上是将其添加到 MdiClient 控件的控件集合中。

  • When you ask MdiChildren of an MDI parent form, it returns the child forms of the MdiClient.当您询问 MDI 父窗体的MdiChildren时,它返回 MdiClient 的子窗体 forms。

  • A form or a control, can have only one parent at time, and when you add it to controls collection of new parent, it will be removed from controls collection of the old parent.一个窗体或一个控件,一次只能有一个父级,当您将它添加到新父级的控件集合中时,它将从旧父级的控件集合中删除。

Now I believe, it's clearer:现在我相信,它更清楚了:

frm.MdiParent = this;
...
...
panelDeskTop.Controls.Add(frm);

The last line, removes the form from MdiChildren of the parent.最后一行从父级的 MdiChildren 中删除表单。 That's why the array is empty.这就是数组为空的原因。

Do you really need MDI?你真的需要MDI吗?

It looks like you don't need MDI.看起来你不需要 MDI。 If the child form is gonna fill the main panel without showing titlebar, then it basically means it doesn't need an mdi parent.如果子窗体将填充主面板而不显示标题栏,那么它基本上意味着它不需要 mdi 父级。 Just make it a non-toplevel and add it to the controls collection of the main panel and show it.只需将其设为非顶层并将其添加到主面板的控件集合中并显示即可。

Resizable sidebar for MDI parent MDI父级可调整大小的侧边栏

If you want have a resizable sidebar for MDI parent, then it's enough to Dock a panel and the splitter to left of the parenr, and then the right side will be occupied by the MdiClient.如果你想为 MDI parent 提供一个可调整大小的侧边栏,那么将面板和拆分器停靠在 parenr 的左侧就足够了,然后右侧将被 MdiClient 占据。 For example:例如:

var mdiParent = new Form() { Size = new Size(700, 450), Text = "parent" };
mdiParent.Load += (obj, args) =>
{
    mdiParent.IsMdiContainer = true;
    mdiParent.Controls.Add(new Splitter()
    { Dock = DockStyle.Left, BackColor = Color.White, Width = 8 });
    mdiParent.Controls.Add(new Panel()
    { Dock = DockStyle.Left, BackColor = Color.Black });
    var child1 = new Form()
    { MdiParent = mdiParent, Text = "child1", Size = new Size(400, 300) };
    var child2 = new Form()
    { MdiParent = mdiParent, Text = "child2", Size = new Size(400, 300) };
    child1.Show();
    child2.Show();
};
mdiParent.ShowDialog();

在此处输入图像描述

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

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