Click here to Skip to main content
Click here to Skip to main content

Menu handler for Most Recently Used files

, 4 Aug 2004
Rate this:
Please Sign up or sign in to vote.
A simple class for implementing Most Recently Used files functionality for multiple situations.

Sample Image - mru_files.jpg

Introduction

Unlike MFC, .NET and C# application projects don't provide automatic support for Most Recently Used files. The MRU list is the last n files opened by the application, with the most recent on top. This is very handy in a .NET application, since the JIT compiler grinds away the first time a file dialog is opened, and the MRU list avoids that most of the time.

The MRU list is typically at the bottom of the File (usually the first) sub-menu, and is below a separator. This article discusses a class which maintains the menu, remembers everything between sessions, and requires minimal modification of existing code.

Background

The need for the MRU functionality came about when the project was almost complete (last minute request by the customer). In this instance, the project had three separate windows, each loading their own file type, necessitating implementation of the MRU functionality three times. To minimize the implementation impact on tested code, and minimize the overall time required, a major goal of the project was to require the minimum interaction with existing code.

Using the code

The source code consists of a single class, MostRecent, and the file contains enough comments that the inner workings should be obvious. For each MRU menu implementation, there are four interactions with the class; three required and one optional. These interactions will be illustrated using the code supplied in the demo project.

The demo project menu has a Load item in the menu, which will load a file (preferably a small text file) and display it in the large text box. The MRU functionality will add files to the list as they are opened.

The MRU functionality is implemented through an instantiation of the MostRecent class. The constructor is used to set most of the information required:

MRUHandler = new 
    Most_Recent.MostRecent(Most_Recent.MostRecent.RecentType.DesignMRU, 
    FileMnu, new Most_Recent.FileToLoad(LoadFile));

Where:

  1. DesignMRU is from an enum which specifies which window is implementing this instance. If your project only has one window, then this can be eliminated.
  2. FileMnu is the sub-menu where the MRU files will be listed.
  3. LoadFile is the function to be called when one of the MRU entries in the menu is clicked. The signature for this function must match the delegate below, declared in MostRecent.
    public delegate void FileToLoad(string fil);

Whenever the application loads a file that is not in the MRU list, this file information must be forwarded to the MostRecent object. This is done with the Add function, as shown below. Once this is done, the new file is now listed in the MRU section of the File menu.

private void LoadMnu_Click(object sender, System.EventArgs e)
{
    OpenFileDialog dlg = new OpenFileDialog();
    if (dlg.ShowDialog() == DialogResult.OK)
    {
        MRUHandler.Add(dlg.FileName);
        LoadFile(dlg.FileName);
    }
}

The third mandatory implementation has already been mentioned above, and refers to the callback function used when one of the MRU items is clicked.

void LoadFile(string filPath)
{
    try
    {
        StreamReader sr = new StreamReader(filPath);
        MainTxt.Text = sr.ReadToEnd();
    }
    catch (Exception ex)
    {
        string err = string.Format("Unable to load {0}: {1}",
            filPath, ex.Message);
        MessageBox.Show(err);
    }
}

The fourth optional implementation is setting the maximum number of entries for the MRU list. As can be seen from the following snippet from MostRecent, the value can be any number greater than or equal to zero. If this functionality is not used, then the value will the default (4 in the current code).

public int MaxCount
{
    set
    {
        mxCount = value;
        if (mxCount < 0)
            mxCount = 0;
        UpdateMax();
    }
    get
    {
        return mxCount;
    }
}

Design decisions

The MostRecent class has a GetFileName function which returns just the file title from the file path, and the title is used in the MRU section of the menu. For the original use of MostRecent, this was more than adequate, but the function can obviously be modified to return greater portions of the file path, including the entire path.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

TDWiseley
Web Developer
United States United States
Born-again keyboard junkie, currently designing, developing and debugging applications for the Windows environment.

Comments and Discussions

 
GeneralI modified the code somewhat and this also uses user.config Pinmemberrickcr29-Nov-05 11:21 
GeneralRe: I modified the code somewhat and this also uses user.config Pinmemberrickcr29-Nov-05 18:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 5 Aug 2004
Article Copyright 2004 by TDWiseley
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid