Zebra0.com

csharp listbox

Ask To Save

There are several times when we need to ask the user if they want to save.

  1. When they select mnuNew.
  2. When they select mnuOpen.
  3. When they select mnuExit, or try to close the program using the X at the top right corner.

Write the code for AskToSave as shown below:

private Boolean AskToSave()
{
   // If there is no change to list don't ask, don't save, and don't cancel
   DialogResult result= DialogResult.No;
   if (changed)
   {
      result = MessageBox.Show("Do you want to save changes to " + currentFileName + "?", "Closing", MessageBoxButtons.YesNoCancel);
      // If they answer yes, write the file
      if (result == DialogResult.Yes) writefile();
   }
   // Returns true if they selected cancel. 
   // Returns false if there has been no change.
   return (result== DialogResult.Cancel);
}
To Do: Think about when you would call this.

End of lesson, Next lesson: ListBox Manager Part 2: Printing in C#