Handling SaveFileDialog with SaveDialogManager

Download SaveDialogManager.zip (19 KB)

I hate re-inventing the wheel. If I find myself repeating code between projects I consider it a good time to build a component… so here it is, the SaveDialogManager class.

The purpose is simple; to decide when to present the user with the SaveFileDialog in an application that supports New, Open, Save, SaveAs and Exit. This kind of decision logic is common to most applications, the program flow looks something like this:

The SaveDialogManager class contains its own copies of the OpenFileDialog and SaveFileDialog objects, but you won’t be invoking these directly although I have made them publicly visible in case you need to adjust their settings.

There are only three events to hook up: Creating, Opening and Saving. Each event is raised after the SaveFileDialog has been shown.

private SaveDialogManager saveManager;

public Main()
{
  InitializeComponent();
      
  // Initialize save dialog manager
  saveManager = new SaveDialogManager("Project", "Project Files (*.pro)|*.pro");
  saveManager.Creating += new EventHandler(saveManager_NewFile);
  saveManager.Opening += new SaveDialogManager.PathEventHandler(saveManager_Opening);
  saveManager.Saving += new SaveDialogManager.PathEventHandler(saveManager_Saving);
}

All your menu handler code needs to do is call the appropriate method on the SaveDialogManager. This will trigger the “Do you want to save changes to xxx?” if the current file is not saved (SaveDialogManager tracks this for you, but you can manually set it using the Unsaved property).

private void ButtonHandler(string command)
{
  switch (command)
  {
    case "New": saveManager.New(); break;
    case "Open": saveManager.Open(); break;
    case "Save": saveManager.Save(); break;
    case "SaveAs": saveManager.SaveAs(); break;
    case "Exit": saveManager.Exit(); break;
  }
}

When the NewFile event is raised, you can assume the active file has either been saved, or the user has opted to lose the changes. You can safely throw away the current file and open a blank one.

private void saveManager_NewFile(object sender, EventArgs e)
{
  // Open a blank file
}

The Opening event handler:

private void saveManager_Opening(object sender, PathEventArgs e)
{
  // Open the selected file
  // TODO: The path is provided in the e.Path property

  // Update the form title
  this.Text = string.Format("{0} - {1}", e.FileName, Application.ProductName);

The Saving event handler:

private void saveManager_Saving(object sender, PathEventArgs e)
{
  // Save the current file
  // TODO: The path is provided in the e.Path property

  // Update the form title
  this.Text = string.Format("{0} - {1}", e.FileName, Application.ProductName);
}

A compiled DLL and the original source code are available for download:

Download SaveDialogManager.zip (19 KB)

Advertisements
This entry was posted in Reference and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s