C# MDI Parent 检测 MDI Child 何时关闭?
我试图在 MDI 父窗体上检测我的 MDI 子窗体何时关闭,并做出相应的反应。在 MDI 子级关闭之前,MDI 父级不应执行任何操作。这是我的代码,我不确定我做错了什么,但是我添加的表单关闭事件方法永远不会被调用...
以下代码位于 MDI 父类中,如果这不是很明显的话。
private void keyValidation()
{
if (Properties.Settings.Default.Unlock == true)
return;
else
{
menu.Enabled = false;
statusStrip.Enabled = false;
ValidationForm vf = new ValidationForm();
vf.MdiParent = this;
vf.Show();
vf.FormClosed += new FormClosedEventHandler(validationForm_FormClosed);
}
}
void validationForm_FormClosed(object sender, FormClosedEventArgs e)
{
MessageBox.Show("Got here");
if (Properties.Settings.Default.Unlock == true)
{
menu.Enabled = true;
statusStrip.Enabled = true;
}
}
感谢您的帮助!
I'm attempting to detect, on the MDI parent, when my MDI child form closes, and react accordingly. The MDI parent shouldn't do anything until the MDI child closes. Here is my code, I'm unsure as to what I'm doing wrong, but the form closed event method I added is never being called...
The following code is in the MDI parent class, if that wasn't obvious.
private void keyValidation()
{
if (Properties.Settings.Default.Unlock == true)
return;
else
{
menu.Enabled = false;
statusStrip.Enabled = false;
ValidationForm vf = new ValidationForm();
vf.MdiParent = this;
vf.Show();
vf.FormClosed += new FormClosedEventHandler(validationForm_FormClosed);
}
}
void validationForm_FormClosed(object sender, FormClosedEventArgs e)
{
MessageBox.Show("Got here");
if (Properties.Settings.Default.Unlock == true)
{
menu.Enabled = true;
statusStrip.Enabled = true;
}
}
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
也许,只是也许,您有一个隐藏而不是关闭的子表单。尝试挂接 VisibleChanged 并看看会发生什么。
此外,FormClosing 将允许您取消关闭,而 FormClosed 不会为您提供该选项。
Maybe, just maybe, you have a CHILD form that HIDES not CLOSES. Try hooking VisibleChanged and see what happens.
Also, FormClosing will allow you to cancel closing, and FormClosed doesn't give you that option.
不要使用
Closed
事件。相反,请使用FormClosing
事件:Don't use the
Closed
Event. Instead, use theFormClosing
event:当通过将 IsMdiContainer 设置为 true 将 ParentForm 标记为 MdiContainer 时,将引发 ParentForm.ControlAdded 事件以将“MdiClient”控件添加到父窗体。因此,当将 MdiClient 添加到父 MDI 表单时,我们可以为 MdiClient 控件引发 ControlAdded 事件,如下所示,
我们需要如下引发 MdiClient.ControlAdded ,
默认情况下 MDI子窗体被添加到父窗体中 MdiClient 的控件集合中。因此,当将 ChildForm.MdiParent 值设置为父窗体时,将引发 MdiClient 的 ControlAdded 事件。
在上面的 MdiClient_ControlAdded 方法中,当子表单添加到父 MDI 表单中时,会引发该方法。因此,通过引发子表单的 FormClosing 和 FormClosed 事件,您可以轻松检测子表单是否关闭。
When mark the ParentForm as MdiContainer by setting the IsMdiContainer to true, the ParentForm.ControlAdded event 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 control as like below,
We need to raise the MdiClient.ControlAdded as like the below,
By default the MDI Child forms are added into the controls collection of the MdiClient in Parent form. So when set the ChildForm.MdiParent value as Parent form, the ControlAdded event for the MdiClient will raise.
In this above MdiClient_ControlAdded method raises when child form added into the Parent MDI form. So by raising the FormClosing and FormClosed events for child forms you can easily detect whether the child form is closed or not.
当您只需调用 MDI Parent 到 MDI Child 的事件处理程序时,为什么要做这么多事情?
假设我想要在我的孩子被激活或停用时做某些事情。您只需要声明 MDI Parent 的一些事件处理程序(例如
SetupToolStripMenu_PlantMasterRecipe
)并提及您想要执行的操作中的一些逻辑。通过这种方式,您可以从 MDI Parent 控制 MDI Child 的每个对象行为。这里 objB 是我的子窗体,我在 MDI 父窗体中编写以下代码:
Why you are doing so many things when you can just invoke an event handler of MDI Parent to MDI Child?
Suppose I want to do certain things when my child will be activated or deactivated. You just need to declare some event handlers of MDI Parent (as e.g.
SetupToolStripMenu_PlantMasterRecipe
) and mention some logic inside that what you want to do. By this way, you can control each object behaviour of MDI Child from MDI Parent.Here objB is my Child form and I am writing this code inside MDI Parent:
在 mdiparent 上创建一个公共函数
然后在子窗体上您可以像这样运行该函数
On mdiparent make a public function
Then on childform you can run the function like this
虽然这并不能真正解决您所指的问题,但从用例来看,您可能需要考虑将验证表单作为模式对话框而不是作为 MDI 子项打开。
您可以使用表单的
ShowDialog()
Show() 的方法。请记住,ShowDialog()
还可以返回DialogResult
如果您将它们分配给其他表单上的按钮。While this doesn't really address the problem you're referring to, judging from the use case, you may want to consider opening the Validation form as a modal dialog instead of as an MDI child.
You can do this using the form's
ShowDialog()
method where you haveShow()
now. Keep in mind thatShowDialog()
can also return aDialogResult
if you assign them to buttons on the other form.