AllTheTimeWorld.com

csharp listboxList Box Editor: Step 7, Save and SaveAs

List Box Editor: Step 7, Save and SaveAs

List Box Editor: Step 7, Save and SaveAs

Save and SaveAs

When the user selects either Save or SaveAs, there are two possibilities:

  1. It is a new file and the filename is "Untitled"
  2. This is a file that was opened and has a filename.

In either case, if the filename is "Untitled" we must ask for a filename. If it is saveAs we also must ask for a filename.

There is another possibility we must consider: The user selects SaveAs, but then selects Cancel when we ask for the filename.

Write the code for mnuSave and mnuSaveAs as shown below.

private void mnuSave_Click(object sender, EventArgs e)
{
   // Save the file.
   writefile();
}
        
private void mnuSaveAs_Click(object sender, EventArgs e)
{ 
    // Save the current file name in case the user selects cancel.
    String file = currentFileName;
    // Set to "Untitled" to signal writefile to ask for filename. 
    currentFileName = "Untitled";
    writefile();
    // Restore filename if user canceled.
    if (currentFileName == "Untitled") currentFileName = file;
}

To Do: Make sure this works before proceeding.