Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prompt user to save when closing app

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;
        }
    }
}
like image 680
Jon B Avatar asked Jan 20 '09 20:01

Jon B


2 Answers

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.

like image 197
recursive Avatar answered Oct 01 '22 04:10

recursive


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.

like image 45
ISW Avatar answered Oct 01 '22 02:10

ISW