其他分享
首页 > 其他分享> > CodeGo.net>如何检测父窗体取消FormClosing事件?

CodeGo.net>如何检测父窗体取消FormClosing事件?

作者:互联网

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

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

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

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

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

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

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

解决方法:

我将提供基于接口的解决方案.通过这种方式,您可以轻松地以统一的方式管理应用程序是否可以关闭.通过以下实现,父窗体负责询问子窗口是否准备好关闭,子窗体执行必须执行的所有操作并回复到主窗口.

假设我有IManagedForm接口:

interface IManagedForm
{
    bool CanIBeClosed(Object someParams);
}

两种形式(Form1和ChildForm)都将实现它.

请注意,对于此示例,我以这种方式实例化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;
}

最后,接口的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);
}

希望它足够清楚.

标签:event-handling,formclosing,childwindow,c,winforms
来源: https://codeday.me/bug/20191201/2078381.html