winforms - MDI表单中的消息框在应用程序关闭之前多次显示

时间:2013-01-28 15:47:14

标签: c# winforms visual-studio-2010 mdi

我知道在StackOverflow中也存在类似问题的例子,但我很难理解问题的本质,因此 - 解决我的自定义案例。

我现在所拥有的是这段代码:

bool closingPending = false;
private void MyFormN_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (closingPending) return;
            //DialogResult answer = MessageBox.Show("Do you want to save changes ?", "Save",
            if (MessageBox.Show("Do you want to save changes ?", "Save",
                                   MessageBoxButtons.YesNoCancel,
                                   MessageBoxIcon.Information,
                                   MessageBoxDefaultButton.Button3) == DialogResult.Yes)
            {
                closingPending = true;
                MessageBox.Show("To Do - validate and save");
            }
            if (MessageBox.Show("Do you want to save changes ?", "Save",
                                   MessageBoxButtons.YesNoCancel,
                                   MessageBoxIcon.Information,
                                   MessageBoxDefaultButton.Button3) == DialogResult.Cancel)
            {
                closingPending = true;
                e.Cancel = true;
            }
            if (MessageBox.Show("Do you want to save changes ?", "Save",
                                   MessageBoxButtons.YesNoCancel,
                                   MessageBoxIcon.Information,
                                   MessageBoxDefaultButton.Button3) == DialogResult.No)
            {
                closingPending = true;
                 Application.Exit();
            }
        }

这是我的尝试和我从其他帖子获得的一些想法的结果。但是当我执行这段代码时会发生什么 - 通过按下窗口的[x],我会看到显示的消息框,但无论我点击哪个按钮,表单都会多次显示。在添加closingPending之前,我很确定只有在我尝试Application.Exit()时才会出现此问题。当我尝试这个例子时closingPending例子工作了:

if(closingPending) return;
if (MessageBox.Show("This application is closing down because of " + e.CloseReason.ToString() + ". Do you really want to close it ?", "", MessageBoxButtons.YesNo) == DialogResult.No)
    {                
        e.Cancel = true;
    }
    else
    {
        closingPending = true;
        Application.Exit();
    }

然而,我对导致问题的原因有了一些了解,但我仍然远远没有完全理解它或解决它。

1 个答案:

答案 0 :(得分:2)

尝试类似下面的内容......它会解决您的问题......

DialogResult result = MessageBox.Show("Do you want to save changes ?", "Save",
                                   MessageBoxButtons.YesNoCancel,
                                   MessageBoxIcon.Information,
                                   MessageBoxDefaultButton.Button3);

            switch (result)
            {

                case DialogResult.Yes: 
                    closingPending = true;
                    MessageBox.Show("To Do - validate and save");
                    break;

                case DialogResult.No: 
                    closingPending = true;
                    Application.Exit();
                    break;

                case DialogResult.Cancel:
                    closingPending = true;
                    e.Cancel = true;
                    break;
            }