Prompt user to save when closing app

后端 未结 5 946
耶瑟儿~
耶瑟儿~ 2021-01-13 09:12

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

相关标签:
5条回答
  • 2021-01-13 09:52

    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.

    0 讨论(0)
  • 2021-01-13 09:53

    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.

    0 讨论(0)
  • 2021-01-13 09:54

    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.

    0 讨论(0)
  • 2021-01-13 09:57

    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.

    0 讨论(0)
  • 2021-01-13 09:58

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

    0 讨论(0)
提交回复
热议问题