提示用户关闭应用程序时,保存 [英] Prompt user to save when closing app

查看:122
本文介绍了提示用户关闭应用程序时,保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写什么归结为一个文档编辑器。当应用程序被关闭,我需要提示用户保存更改。这是很容易的。我的问题是什么时候适当的没有的提示用户,而是简单地放弃未保存的数据和关闭。

在的FormClosing事件中,CloseReason枚举包括:

  • 在无
  • WindowsShutDown
  • MdiFormClosing
  • UserClosing
  • TaskManagerClosing
  • FormOwnerClosing
  • ApplicationExitCall

我想,WindowsShutDown和TaskManagerClosing应该不会导致一个保存更改?提示出现,以prevent应用程序挂起与提示显示。

这是标准做法,或者我应该做点别的吗?

为了清楚起见,这里的code:

 保护覆盖无效OnFormClosing(FormClosingEventArgs E)
{
    base.OnFormClosing(E);

    如果(!(e.CloseReason == CloseReason.WindowsShutDown || e.CloseReason == CloseReason.TaskManagerClosing)
            &功放;&安培; this.ChangesPending())
    {
        开关(的MessageBox.show(这一点,保存更改?,保存更改,MessageBoxButtons.YesNoCancel))
        {
            案例DialogResult.Yes:
                this.Save();
                打破;
            案例DialogResult.No:
                // 没做什么
                打破;
            案例DialogResult.Cancel:
                e.Cancel =真;
                打破;
        }
    }
}
 

解决方案

我觉得 TaskManagerClosing 应该是不会提示,如有的唯一原因。就个人而言,我希望在 WindowsShutDown 的事件提示。如果我关闭Windows有一个未保存的文档的地方,这意味着我已经忘了。

I'm writing what boils down to a document editor. When the application is closing, I need to prompt the user to save changes. This is easy enough. My question is when is it appropriate to not prompt the user, and instead simply discard unsaved data and close.

In the FormClosing event, the CloseReason enum includes:

  • None
  • WindowsShutDown
  • MdiFormClosing
  • UserClosing
  • TaskManagerClosing
  • FormOwnerClosing
  • ApplicationExitCall

I figure that WindowsShutDown and TaskManagerClosing should not cause a "save changes?" prompt to appear, to prevent the app from hanging with that prompt showing.

Is this standard practice, or should I be doing something else here?

For clarity, here's the code:

protected override void OnFormClosing(FormClosingEventArgs e)
{
    base.OnFormClosing(e);

    if (!(e.CloseReason == CloseReason.WindowsShutDown || e.CloseReason == CloseReason.TaskManagerClosing)
            && this.ChangesPending())
    {
        switch (MessageBox.Show(this, "Save changes?", "Save Changes", MessageBoxButtons.YesNoCancel))
        {
            case DialogResult.Yes:
                this.Save();
                break;
            case DialogResult.No:
                // Do nothing
                break;
            case DialogResult.Cancel:
                e.Cancel = true;
                break;
        }
    }
}

解决方案

I think TaskManagerClosing should be the only reason that does not prompt, if any. Personally, I would want to be prompted in the event of WindowsShutDown. If I'm shutting down Windows with an unsaved document somewhere, it means I've forgotten about it.

这篇关于提示用户关闭应用程序时,保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆