Prompt user to save when closing app

倾然丶 夕夏残阳落幕 提交于 2019-12-01 03:28:41

问题


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;
        }
    }
}

回答1:


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.




回答2:


I'd definitly also show the "Do you want to save" dialog on WindowsShutDown (the application might e.g. have been in the background for some time and the user forgot about it, or he might have clicked on "Restart" after a service pack was installed without thinking twice etc.).

As for TaskManagerClosing, I'd not show the dialog in this case.




回答3:


I personally prefer to just close the program when the user selects close, if there are any unsaved documents at close time I prefer to keep backup copies of those files and inform the user that there are unsaved documents when they next open the application.

I do this for a couple of reasons, number one I like my applications to close when I tell them to close, and number two by keeping a temporary copy of the file that I update with all the changes as the user works on it I safeguard against unexpected crashes and closes of my applications.

So with this technique you don't need to worry about ow the application has been closed.




回答4:


Really the CloseReason is a moot point, isn't it? The fact that your form is going away is what you're trying to catch.

Now you need to know if your application has already handled the "save" event. If so, the form can go away. You've saved your document. But if not, you may want to prompt the user.

If you can validate the data quickly (i.e. do a string compare or hash compare on the document compared to the data in the file) then you'll know if the user has saved the form data.

Otherwise, if there are alot of fields, and checking each one is resource-prohibitive, then put a "isDirty" flag on your form. Let the Save() method set the isDirty to false, and every other field change sets it to true.

Then, in formClosing, all you need is :

protected override void OnFormClosing(FormClosingEventArgs e)
{
    if (isDirty) 
    {
        DialogResult R = MessageBox.Show(this, "Save changes?", "Save Changes", 
                MessageBoxButtons.YesNoCancel);

        if (R == DialogResult.Yes)
        {
            this.Save();
        } else if (R == DialogResult.Cancel)
        {
            e.Cancel = true;
        }
    }
}




回答5:


I'd think even TaskManagerClosing could use a prompt to save. If your app is responding normally, closing it through the task manager should be no different from any other way of closing it. If it's hung, it doesn't matter what your onClose handler does - it'll never get there.

I agree with @Jerry, in that it's more important to avoid prompting to save if the data hasn't changed since the last save. I usually use a simple 'changed' flag, which gets set on any edit action, and cleared on saves and loads.



来源:https://stackoverflow.com/questions/462903/prompt-user-to-save-when-closing-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!