Click here to Skip to main content
15,897,518 members
Articles / Desktop Programming / Windows Forms

FileSelect - Hassle Free Implementation of the File Menu

Rate me:
Please Sign up or sign in to vote.
4.67/5 (15 votes)
4 May 2009CPOL13 min read 51.5K   512   39  
A WinForms user control that implements the details of file handling commands for any document-centric application
// ==================================================================
// 
//  FileSelectDemo - FileSelectDemo.cs
// 
//  Copyright 2009 Peter Hauptmann
//  This code is published under CodeProject Open License 
//    see http://www.codeproject.com/info/cpol10.aspx
//  Latest version and feedback at
//   http://www.codeproject.com/KB/menus/fileselect.aspx 
// 
// ------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using PH.UI.FileSelect;
using System.Diagnostics;
using TestFileSelect.Properties;

namespace TestFileSelect
{
  public partial class FileSelectDemo : Form
  {
    public FileSelectDemo()
    {
      InitializeComponent();

      // load "recent files" list:
      // Settings.Default.Reload();
      fileSelect1.RecentFiles.AllFiles = Settings.Default.RecentFileList;
    }

    private void fileSelect1_NewOrOpenDocument(object sender, EventArgs e)
    {
      // this handles both the "create new" and "open existing" document
      // we put the code into a single handler, since it is very similar.

      FileSelectEventArgs fse = (FileSelectEventArgs)e;
      DocumentInfo docInfo = fse.DocumentInfo;

      // just a check, that my FileSelect implementation is correct (you don't usually need that):
      // in single file mode, there should be no current document when "new" is called.
      Debug.Assert(textBox1.Text.Length == 0); 

      // Create document instance
      TextDocument textDoc;
      if (fse.Command == EFSCommand.New)
        textDoc = TextDocument.New();
      else
        textDoc = TextDocument.Load(fse.Path);

      // if that is successful, initialize the document info with the document
      docInfo.InitDocument(textDoc);

      // update the user interface:
      textBox1.Text = textDoc.Data;
      textBox1.Tag = docInfo;
      textBox1.ReadOnly = false;
    }
    private void fileSelect1_CloseDocument(object sender, EventArgs e)
    {
      // this event fires to close a document.
      // we clear the TextBox, and mark it as "read only"
      textBox1.Text = String.Empty;
      textBox1.Tag = null;
      textBox1.ReadOnly = true;
    }
    private void fileSelect1_SaveDocument(object sender, EventArgs e)
    {
      // we update the document, and save to the path specified in the event args

      FileSelectEventArgs fse = (FileSelectEventArgs)e;
      DocumentInfo docInfo = fse.DocumentInfo;
      TextDocument doc = (TextDocument)docInfo.Document;

      Debug.Assert(docInfo == textBox1.Tag); // another sanity check - this should be given in a single document single view scenario

      // update the document with changes from the view, and save it
      doc.Data = textBox1.Text;
      doc.Save(fse.Path); // note: fse.Path may be different from docInfo.Path
      fse.SaveComplete = true; // indicate that save was successful
    }
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
      // handle changes in the view
      // Note: Here, we only set the document dirty. 
      // Since we have a single view, the change will be published to the document 
      // class just before saving. 
      DocumentInfo docInfo = textBox1.Tag as DocumentInfo;
      if (docInfo != null)
        docInfo.IsDirty = true;
    }


    // basic EXIT handler
    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
      Close();
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
      if (!fileSelect1.HandleQuit(e.CloseReason))
      {
        e.Cancel = true;
        return;
      }

      // save settings
      Settings.Default.RecentFileList = fileSelect1.RecentFiles.AllFiles;
      Settings.Default.Save();
    }

    private void propertyGrid1_Click(object sender, EventArgs e)
    {

    }

  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Klippel
Germany Germany
Peter is tired of being called "Mr. Chen", even so certain individuals insist on it. No, he's not chinese.

Peter has seen lots of boxes you youngsters wouldn't even accept as calculators. He is proud of having visited the insides of a 16 Bit Machine.

In his spare time he ponders new ways of turning groceries into biohazards, or tries to coax South American officials to add some stamps to his passport.

Beyond these trivialities Peter works for Klippel[^], a small german company that wants to make mankind happier by selling them novel loudspeaker measurement equipment.


Where are you from?[^]



Please, if you are using one of my articles for anything, just leave me a comment. Seeing that this stuff is actually useful to someone is what keeps me posting and updating them.
Should you happen to not like it, tell me, too

Comments and Discussions