如何检测父窗体取消FormClosing事件?

时间:2012-07-03 06:33:43

标签: c# winforms event-handling childwindow formclosing

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

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

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

当用户尝试关闭主窗体时 - 如果子窗体打开 - 则ChildForm中首先触发FormClosing事件,然后在主窗体中引发相同的事件。在FormClosing事件中,子表单将停止其绘制线程。

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

然而,到那时,子表单的FormClosing事件已经被引发,它将停止其绘制线程。子表单不知道它现在应该继续绘制(好像什么都没发生过)。

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

3 个答案:

答案 0 :(得分:2)

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

假设我有接口IManagedForm

interface IManagedForm
{
    bool CanIBeClosed(Object someParams);
}

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

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

希望它足够清楚。

答案 1 :(得分:1)

嗯,是/否/取消的 desicional 逻辑移动到某个cetral类。
并且 Child FormClosing Parent 都会调用相同的函数。

当然,为了获得良好的用户体验,如果从FormClosing函数调用函数,则必须以只执行一次事件的方式管理它。

答案 2 :(得分:0)

MSDN将FormClosing事件解释为在表单关闭之前调用的事件。接着说,对于MDI,在调用父窗体FormClosing事件之前调用所有子窗体FormClosing事件。

因此,这意味着,当在主窗体上按下关闭按钮时,它会为所有子窗体引发FormClosing事件。因此子表单应确定是否已准备好关闭,并相应地设置cancel属性。当为主表单引发事件时,它应该已经设置为取消。

主表单不应该决定子表单,即使在主表单上引发了close事件。

进一步阅读,

Explanation on Form.FormClosing event at MSDN

Explaining how the cancel property can be used