【问题标题】:Event handler used to close a Windows Forms application用于关闭 Windows 窗体应用程序的事件处理程序
【发布时间】:2013-11-01 03:52:25
【问题描述】:

我正在尝试创建一个event handler,这样当用户选择close (x) 按钮时,它会提示用户保存任何未保存的更改。这是我的C# 代码:

private void CloseFileOperation()
{
    // If the Spreadsheet has been changed since the user opened it and
    // the user has requested to Close the window, then prompt him to Save
    // the unsaved changes.
    if (SpreadSheet.Changed)
    {
        DialogResult UserChoice = MessageBox.Show("Would you like to save your changes?", "Spreadsheet Utility",
                MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);

        switch (UserChoice)
        {
            case DialogResult.Yes:
                SaveFileOperation();
                this.Close();
                break;
            case DialogResult.No:
                this.Close();
                break;
            case DialogResult.Cancel:
                return;
        }
    }

    // If the Spreadsheet hasn't been changed since the user opened it, then
    // simply Close the window.
    else
        this.Close();
}

我创建了当用户选择关闭 (x) 按钮时触发的事件处理程序 MainFram_FormClosing

private void MainFrame_FormClosing(object sender, FormClosingEventArgs e)
{
    // Close the Spreadsheet.
    CloseFileOperation();
}

每当我选择关闭按钮时,应用程序就会崩溃。我已经阅读了来自this 帖子的回复。我想我违反了Windows 7 Program Requirements。我想我不明白为什么这个功能不能这么容易地完成。

这方面最好的方法是什么?

【问题讨论】:

  • MainFram_FormClosing 事件中设置一个断点并检查。
  • 如果这么简单,它就不会出现在 Stack 上。事件触发的时候好像一直在执行CloseFileOperation。

标签: c# winforms forms validation savechanges


【解决方案1】:

如果您使用事件处理程序本身而不是单独的例程,则可以访问 FormClosingEventArgs,这将允许您在必要时取消关闭。此外,您正在使用this.Close();,它只是重新启动事件,而不是返回并让事件完成。当我将此代码集用作事件处理程序时,它可以按预期在 Win7 上运行:

    private void Form2_FormClosing(object sender, FormClosingEventArgs e)
    {
        if(SpreadSheet.Changed)
        {
            switch(MessageBox.Show("Would you like to save your changes?", "Spreadsheet Utility",
                MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning))
            {
                case DialogResult.Yes:
                    SaveFileOperation();
                    return;
                case DialogResult.No:
                    return;
                case DialogResult.Cancel:
                    e.Cancel = true;
                    return;
            }
        }
    }

【讨论】:

    【解决方案2】:

    我的答案是:

    1.不要调用this.close()函数,因为你已经在关闭事件了。

    2.如果你想改变关闭事件的动作(关闭或不关闭)你可以简单地设置FormClosingEventArgs参数(e在下面的代码)属性canceltrue 取消关闭或 false 关闭。

    3.如果工作表没有保存,你不需要做任何事情,在这种情况下,表单应该在没有提示的情况下简单地关闭。因此你可以忽略 else 块。

    这里修改后的代码是:

    如果 (SpreadSheet.Changed)

                {
                    DialogResult UserChoice = MessageBox.Show("Would you like to save your changes?", "Spreadsheet Utility",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Warning);
    
                    switch (UserChoice)
                    {
                        case DialogResult.Yes:
                           SaveFileOperation();
                            break;
                    case DialogResult.No:
                        break;
                    case DialogResult.Cancel:
                        e.Cancel = true;
                        break;
    

    【讨论】:

      猜你喜欢
      • 2015-07-21
      • 2016-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多