C# 添加或删除带有子表单的 MDI 表单检测
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/538204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MDI Form detecting with a child form is added or removed
提问by Malfist
Is there an event I can use to tell if a child form has been added or removed from the MDI parent?
是否有一个事件可以用来判断子窗体是否已从 MDI 父窗体中添加或删除?
采纳答案by casperOne
No, there is not. You would have to subclass Form and expose specific events that would indicate when the child is added and then route all attachments of child forms through a method that would wire up the child form, as well as raise the event.
不,那里没有。您必须对 Form 进行子类化并公开特定事件,这些事件指示何时添加子表单,然后通过连接子表单并引发事件的方法路由子表单的所有附件。
回答by Chris Holmes
Yes. On your main MDI form, wire up to the MdiChildActivated Event.
是的。在您的主 MDI 窗体上,连接到 MdiChildActivated 事件。
Like so:
像这样:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.MdiChildActivate += new EventHandler(Form1_MdiChildActivate);
}
void Form1_MdiChildActivate(object sender, EventArgs e)
{
MessageBox.Show("Activated");
}
private void addToolStripMenuItem_Click(object sender, EventArgs e)
{
Form form2 = new Form2();
form2.MdiParent = this;
form2.Show();
}
}
And that event will fire when the child form is both activated or deactivated.
当子窗体被激活或停用时,该事件将触发。
回答by lc.
Wire up the MdiChildActivate
event and keep a list of recognized children. When a new form is activated, also wire up the FormClosed
event.
安排MdiChildActivate
活动并保留一份认可儿童的名单。当一个新表单被激活时,也连接FormClosed
事件。
private List<Form> ChildFormList = new List<Form>();
private void MyForm_MdiChildActivate(object sender, EventArgs e)
{
Form f = this.ActiveMdiChild;
if (f == null)
{
//the last child form was just closed
return;
}
if (!ChildFormList.Contains(f))
{
//a new child form was created
ChildFormList.Add(f);
f.FormClosed += new FormClosedEventHandler(ChildFormClosed);
}
else
{
//activated existing form
}
}
private void ChildFormClosed(object sender, FormClosedEventArgs e)
{
//a child form was closed
Form f = (Form)sender;
ChildFormList.Remove(f);
}
回答by Ryan Buddicom
I realised this is many years too late however as the answers here helped me solve this I though I would mention this works fine using the MdiChildren array in .net 4. The only thing you need to do is check if the form is disposing or disposed to tell if its closing.
我意识到这已经晚了很多年但是因为这里的答案帮助我解决了这个问题我虽然我会提到使用 .net 4 中的 MdiChildren 数组可以很好地工作。您唯一需要做的就是检查表单是否正在处理或处理判断它是否关闭。
ie:
IE:
private void frmContainer_MdiChildActivate(object sender, EventArgs e)
{
tabWindows.RefreshLayout(this.MdiChildren.ToList());
}
public void RefreshLayout(List<Form> forms)
{
this.nextButtonLeft = 1;
panel1.Controls.Clear();
foreach (Form frm in forms)
{
if (!(frm.Disposing || frm.IsDisposed)) { addButton(frm); }
}
}
回答by Robbie
I recently wanted to determine approximately when there were NO MDIchildren open so I could display a panel with lots of "things to do" buttons on it Only when there were no child forms open. (just exploring a design idea).
我最近想确定大约什么时候没有 MDIchildren 打开,所以我可以显示一个面板,上面有很多“要做的事情”按钮,只有当没有子窗体打开时。(只是探索一个设计理念)。
My eventual solution was elegantly simple - add a timer to the parent form and start the timer up whenever the MdiChildActivate event determined there was 1 child form open.
我的最终解决方案非常简单 - 向父窗体添加一个计时器,并在 MdiChildActivate 事件确定有 1 个子窗体打开时启动计时器。
private void MyForm_MdiChildActivate(object sender, EventArgs e)
{
this.panel_Tools.Visible = false;
if (this.MdiChildren.Count() == 1)
{
this.timer_WindowsCounter.Start();
}
else
{
this.timer_WindowsCounter.Stop();
}
}
private void timer_WindowsCounter_Tick(object sender, EventArgs e)
{
if (this.MdiChildren.Count() == 0)
{
this.panel_Tools.Visible = true;
this.timer_WindowsCounter.Stop();
}
}
回答by Prashant vishwakarma
private void closeToolStripMenuItem1_Click(object sender, EventArgs e)
{
List<Form> fm = this.MdiChildren.ToList();
if(fm!=null && fm.Count>0)
{
foreach(Form lfm in fm)
{
lfm.Close();
}
}
}
回答by Valvestino
Since the MdiChildActivate event is fired before the MDI child form is actually closed and therefore there isn't enough information to detect if a MDI child form is actually activated or closed, I chose a different approach to solve the problem.
由于 MdiChildActivate 事件在 MDI 子窗体实际关闭之前被触发,因此没有足够的信息来检测 MDI 子窗体是实际激活还是关闭,我选择了不同的方法来解决问题。
I found that the ParentChanged event fires on the MDI child form when it is closed.
我发现 ParentChanged 事件在 MDI 子窗体关闭时触发。
public class MdiParentForm : Form
{
// ...
private Form CreateMdiChildForm()
{
var form = new MdiChildForm
form.ParentChanged += MdiFormParentChangedHandler;
form.MdiParent = this;
return form;
}
private void MdiFormParentChangedHandler(object sender, EventArgs args)
{
var form = sender as Form;
if (form != null)
{
if (form.MdiParent != null)
{
// MDI child form will activate
// ... your code here
}
else
{
// MDI child form will close
form.ParentChanged -= MdiFormParentChangedHandler;
// ... your code here
}
}
}
// ...
}
回答by Jagadeesan
Yes, you can easily detect the forms adding in MDI Form.
是的,您可以轻松检测添加到 MDI 表单中的表单。
When mark the ParentForm as MdiContainerby setting the IsMdiContainer to true, the ParentForm.ControlAddedevent raised for adding the "MdiClient" control to the parent form. So when adding MdiClient to parent MDI form, we can raise the ControlAdded event for the MdiClient controlas like below,
当通过将 IsMdiContainer 设置为 true 将ParentForm标记为 MdiContainer 时,会引发 ParentForm.ControlAdded事件以将“MdiClient”控件添加到父窗体。因此,当将 MdiClient 添加到父 MDI 表单时,我们可以为 MdiClient 控件引发 ControlAdded 事件,如下所示,
public ParentForm()
{
InitializeComponent();
this.ControlAdded += Form1_ControlAdded;
this.IsMdiContainer = true;
We need to raise the MdiClient.ControlAdded as like the below,
我们需要像下面这样提高 MdiClient.ControlAdded,
void Form1_ControlAdded(object sender, ControlEventArgs e)
{
if(e.Control is MdiClient)
e.Control.ControlAdded += MdiClient_ControlAdded;
}
By default the MDI Child forms are added into the controls collection of the MdiClientin Parent form. So when set the ChildForm.MdiParent value as Parent form, the ControlAdded event for the MdiClient will raise.
默认情况下,MDI 子窗体被添加到父窗体中的 MdiClient 的控件集合中。因此,当将 ChildForm.MdiParent 值设置为父表单时,MdiClient的ControlAdded 事件将引发。
void MdiClient_ControlAdded(object sender, ControlEventArgs e)
{
}
So by using the above method, you can know the child MDI forms added into the parent MDI forms. Like this you can add the ControlRemoved event for the MdiClient control to know the child forms removed from MDI form.
所以通过上面的方法,你可以知道子MDI表单添加到父MDI表单中。像这样您可以为 MdiClient 控件添加 ControlRemoved 事件以了解从 MDI 表单中删除的子表单。
Hope this will help you.
希望这会帮助你。