C# 如何检测父窗体取消窗体关闭事件?
我有一个从我的主窗体打开的子窗体,如下所示:C# 如何检测父窗体取消窗体关闭事件?,c#,winforms,event-handling,childwindow,formclosing,C#,Winforms,Event Handling,Childwindow,Formclosing,我有一个从我的主窗体打开的子窗体,如下所示: var cf = new ChildForm { Owner = this }; cf.Show(); 子窗体然后在另一个线程上进行一些绘制 当用户试图关闭主窗体时(如果子窗体处于打开状态),则首先在ChildForm中触发一个事件,然后在主窗体中引发相同的事件。在FormClosing事件中,子窗体停止其绘图线程 当主窗体包含未保存的数据时,用户可以尝试关闭它。然后,主窗体的FormClosing事件处理程序会向它们显示一条警告“数据未保存。取消
var cf = new ChildForm { Owner = this };
cf.Show();
子窗体然后在另一个线程上进行一些绘制
当用户试图关闭主窗体时(如果子窗体处于打开状态),则首先在ChildForm
中触发一个事件,然后在主窗体中引发相同的事件。在FormClosing
事件中,子窗体停止其绘图线程
当主窗体包含未保存的数据时,用户可以尝试关闭它。然后,主窗体的FormClosing
事件处理程序会向它们显示一条警告“数据未保存。取消关闭?”。然后,他们可以取消保存(即主窗体的FormClosing
事件处理程序在对象上设置cancel
标志)
但是,此时,子窗体的FormClosing
事件已经引发,它将停止其绘图线程。子窗体不知道现在应该继续绘制(就好像什么都没有发生一样)
是否可以从子窗体检测到主窗体取消了FormClosing
事件?当用户被要求在主窗体中保存数据时,我仍然希望停止重画线程。好的,Yes/NO/Cancel的决策逻辑移动到某个cetral类。子级和父级的
FormClosing
调用相同的函数
当然,为了获得良好的用户体验,如果从两个
FormClosing
函数调用函数,则必须以仅执行一次事件的方式对其进行管理。MSDN将FormClosing事件解释为在表单关闭之前调用的事件。它接着说,对于MDI,在调用父窗体FormClosing事件之前,将调用所有子窗体FormClosing事件
这意味着,当按下主窗体上的关闭按钮时,它会为所有子窗体引发FormClosing事件。因此,子窗体应该确定它是否准备好关闭,并相应地设置cancel属性。当主窗体引发事件时,应已将其设置为“取消”
即使在主窗体上引发了关闭事件,主窗体也不应为子窗体作出决定
进一步阅读:
我会提供一个基于接口的解决方案。这种方法将很容易为您提供一种统一的方法来管理应用程序是否可以关闭。在下面的实现中,父窗体负责询问子窗口是否已准备好关闭,子窗口执行所有必须执行的操作并回复主窗口 假设我有接口
IManagedForm
:
interface IManagedForm
{
bool CanIBeClosed(Object someParams);
}
这两种表单(Form1
和ChildForm
)都将实现它
注意,对于这个示例,我用以下方式实例化了子表单
:
ChildForm cf = new ChildForm() { Owner = this, Name = "ChildForm" };
cf.Show();
首先是Form1
对接口的实现:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
object someArgsInterestingForTheMethod = new object();
e.Cancel = !((IManagedForm)this).CanIBeClosed(someArgsInterestingForTheMethod);
}
// Ask the ChildForm it is done. If not the user should not leave the application.
public bool CanIBeClosed(object someParams)
{
bool isOKforClosing = true;
var cf = this.Controls["ChildForm"] as IManagedForm;
if (cf != null)
{
isOKforClosing = cf.CanIBeClosed(someParams);
if (!isOKforClosing)
{
MessageBox.Show("ChildForm does not allow me to close.", "Form1", MessageBoxButtons.OK);
}
}
return isOKforClosing;
}
最后,界面的子窗体实现如下所示:
private void ChildForm_FormClosing(object sender, FormClosingEventArgs e)
{
object someArgsInterestingForTheMethod = new object();
e.Cancel = !((IManagedForm)this).CanIBeClosed(someArgsInterestingForTheMethod);
}
public bool CanIBeClosed(object someParams)
{
// This flag would control if this window has not pending changes.
bool meetConditions = ValidateClosingConditions(someParams);
// If there were pending changes, but the user decided to not discard
// them an proceed saving, this flag says to the parent that this form
// is done, therefore is ready to be closed.
bool iAmReadyToBeClosed = true;
// There are unsaved changed. Ask the user what to do.
if (!meetConditions)
{
// YES => OK Save pending changes and exit.
// NO => Do not save pending changes and exit.
// CANCEL => Cancel closing, just do nothing.
switch (MessageBox.Show("Save changes before exit?", "MyChildForm", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question))
{
case DialogResult.Yes:
// Store data and leave...
iAmReadyToBeClosed = true;
break;
case DialogResult.No:
// Do not store data, just leave...
iAmReadyToBeClosed = true;
break;
case DialogResult.Cancel:
// Do not leave...
iAmReadyToBeClosed = false;
break;
}
}
return iAmReadyToBeClosed;
}
// This is just a dummy method just for testing
public bool ValidateClosingConditions(object someParams)
{
Random rnd = new Random();
return ((rnd.Next(10) % 2) == 0);
}
希望足够清楚。就我而言,我没有使用MDI窗口。这似乎是一个非常明智的选择。谢谢,足够清楚,可以很好地演示原理。我应该更清楚的是,子窗体并不总是打开的,所以需要一些额外的管道……这只是了解总体概念的一个开始。注意:如果您的
ChildForm
未打开,则以下条件if(cf!=null)
在此行var cf=this之后。控件[“ChildForm”]作为IManagedForm
将不为true
,子表单
将不会被请求关闭权限,并且主表单将继续其关闭操作。是否有任何进展?你找到解决办法了吗?