关闭所有Mdi容器中的儿童? [英] Close All Childrens From an Mdi Container?
问题描述
大家好,
我有一个 MDI 容器,其中有1个 MenuStrip
Hi All,
I have an MDI container with 1 MenuStrip
New->Form1
Form2
Form3
如何从 MDI 容器关闭所有子窗体,但最后打开的窗体除外.
在 New-> Form1 中,我编写了以下代码来显示 Form1 :
How to close all child forms from an MDI container except the Last Opened Form.
In New->Form1 click I wrote the following code to show Form1:
Form1 fc = new Form1();
fc.TopLevel = false;
fc.MdiParent = this;
fc.Parent = this.splitContainer1.Panel2;
fc.StartPosition = FormStartPosition.CenterParent;
fc.FormBorderStyle = FormBorderStyle.None;
fc.Location = new System.Drawing.Point(0, 70);
fc.ShowInTaskbar = false;
fc.BringToFront();
fc.Show();
Form2 和 Form3 点击相同.
当我打开任何一个菜单项时,如果要打开其余两个菜单,我想关闭它们.
我写了以下代码,但对我不起作用.
Same for Form2 and Form3 clicks.
When I open any one of the menu item, I want to close the remaining two if they are open.
I wrote the following code but it doesn''t work for me.
foreach(Form childForm in MdiChildren)
{
childForm.Close();
}
问候,
帕万.
Regards,
Pawan.
推荐答案
你好帕万,
您的代码存在问题,因为一旦您设置了MDIParent,fc就会立即成为MDI子窗体.但是,一旦将Panel 2设置为父级,则MDIParent设置为null,并且fc不再是MDI子级.
Hello Pawan,
There is a problem with your code in that as soon as you set the MDIParent, the fc immediately becomes an MDI child form. But as soon as you set Panel 2 as the parent, MDIParent is set to null and fc is no longer an MDI child.
fc.MdiParent = this;
// At this point fc is an MDI child.
fc.Parent = this.splitContainer1.Panel2;
// At this point MdiParent is null, and fc is no longer an MDI child.
您需要做的是遍历Panel2控件集合中的表单,然后关闭每个表单,如下所示.
What you need to do is iterate through the forms in the controls collection for Panel2 and close each form as follows.
foreach (Form childForm in this.splitContainer1.Panel2.Controls)
{
childForm.Close();
}
Form1 fc = new Form1();
fc.TopLevel = false;
fc.Parent = this.splitContainer1.Panel2;
fc.StartPosition = FormStartPosition.CenterParent;
fc.FormBorderStyle = FormBorderStyle.None;
fc.BackColor = Color.Green;
fc.Location = new System.Drawing.Point(0, 70);
fc.ShowInTaskbar = false;
fc.BringToFront();
fc.Show();
希望对您有帮助.
杰森.
I hope this helps you.
Jason.
嗨杰森
您的代码可以帮助我解决很多问题,感谢您.
问候,
帕万.
Hi Jason
U r code helps me alot to solve my issue and Thanks to u.
Regards,
Pawan.
您能打电话给Application.Exit
还是简单地END
(仅在VB中)
Hi,
Could you please try by CallingApplication.Exit
or SimplyEND
(Only in VB)
这篇关于关闭所有Mdi容器中的儿童?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!