Prompt user to save when closing app

丶灬走出姿态 提交于 2019-12-01 05:44:30

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.

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.

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.

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

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.

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