gpt4 book ai didi

c# - 如何检测取消 FormClosing 事件的父表单?

转载 作者:行者123 更新时间:2023-11-30 16:23:44 25 4
gpt4 key购买 nike

我从主窗体中打开了一个子窗体,如下所示:

var cf = new ChildForm { Owner = this };
cf.Show();

然后子窗体在另一个线程上进行一些绘制。

当用户试图关闭主窗体时 - 如果子窗体处于打开状态 - 那么 FormClosing事件首先在 ChildForm 中触发,然后在主窗体中引发相同的事件。在 FormClosing 事件中,子窗体停止其绘图线程。

当主窗体包含未保存的数据时,用户可能会尝试关闭主窗体。然后主窗体的 FormClosing 事件处理程序向他们显示警告“数据未保存。取消关闭?”。然后他们可以取消保存(即 Cancel 标志由主窗体的 FormClosing 事件处理程序设置在 FormClosingEventArgs 对象上)。

但是,到那时,子窗体的 FormClosing 事件已经被引发,并且它将停止其绘图线程。子窗体不知道它现在应该继续绘制(就好像什么都没发生过一样)。

是否可以从子窗体中检测到 FormClosing 事件已被主窗体取消?当要求用户在主窗体中保存数据时,我仍然想停止重绘线程。

最佳答案

我会提供一个基于接口(interface)的解决方案。通过这种方式,您可以很容易地有一个统一的方式来管理应用程序是否可以关闭。通过以下实现,父窗体负责询问子窗口是否准备好关闭,子窗口执行任何必须执行的操作并回复主窗口。

假设我有接口(interface)IManagedForm:

interface IManagedForm
{
bool CanIBeClosed(Object someParams);
}

两种形式(Form1ChildForm)都会实现它。

请注意,对于此示例,我以这种方式实例化 ChildForm:

ChildForm cf = new ChildForm() { Owner = this, Name = "ChildForm" };
cf.Show();

首先是Form1接口(interface)的实现:

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;
}

最后,接口(interface)的 ChildForm 实现 将如下所示:

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);
}

希望它足够清楚。

关于c# - 如何检测取消 FormClosing 事件的父表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11305614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com