Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / Windows Forms

Most Recently Used (MRU) Menu Class for .NET 2.0 in C#

Rate me:
Please Sign up or sign in to vote.
4.83/5 (59 votes)
6 Nov 2009CPOL7 min read 221.3K   5K   122   69
A Most Recently Used files class for .NET 2.0 in C#

Introduction

A few years ago, I wrote code and an article to implement a Most Recently Used files class for .NET 1.1. Visual Studio 2005 and .NET 2.0 introduced the MenuStrip class. Unfortunately, they still forgot to add an MRU list, so I modified my original code. I debated just adding the new project to that one, but decided it was best to keep this as a separate article since the previous class is now obsolete.

The original article can be found here.

The MruStripMenu Class

The MruStripMenu class displays a list of the most recently used files in a popup menu. A derived class, MruStripMenuInline, displays them as additional entries in a menu, or inline. In both cases, a number is displayed to the left of the file. Entries 1-10 allow you to use a number on the keyboard to select an entry.

Namespace

This class is in the creatively named, JWC (Joe Woodbury Classes) namespace.

Constructors

C#
MruStripMenu(ToolStripMenuItem recentFileMenuItem, 
    ClickedHandler clickedHandler)
MruStripMenu(ToolStripMenuItem recentFileMenuItem, 
    ClickedHandler clickedHandler, int maxEntries)
MruStripMenu(ToolStripMenuItem recentFileMenuItem, 
    ClickedHandler clickedHandler, string registryKeyName)
MruStripMenu(ToolStripMenuItem recentFileMenuItem, 
    ClickedHandler clickedHandler, string registryKeyName, int maxEntries)
MruStripMenu(ToolStripMenuItem recentFileMenuItem, 
    ClickedHandler clickedHandler, string registryKeyName, 
    bool loadFromRegistry)
MruStripMenu(ToolStripMenuItem recentFileMenuItem, 
    ClickedHandler clickedHandler, string registryKeyName, 
    bool loadFromRegistry, int maxEntries)
  • recentFileMenuItem

    This is the menu item that will serve as the anchor point for the MRU list. It must be created and inserted into the desired spot in a menu, before creating an MruMenu instance.

  • clickedHandler

    The delegate that will be called when one of the MRU entries is selected. The entry number (zero based) and the filename will be passed to the handler.

  • registryKeyName

    The name of a registry key MruMenu that will be used to load and/or store the contents of the MRU list. If a key name is passed to the constructor, and loadFromRegistry is true, MruMenu will attempt to load the files from the registry. However, to save the MRU list, you must call SaveToRegistry().

  • loadFromRegistry

    If true, MruMenu will attempt to load the MRU list stored in the passed registry key. If the registry key does not exist, an exception will not be thrown.

  • maxEntries

    The maximum number of entries to allow in the MRU list. Currently, for practical reasons, the number of entries is limited to be from 4 through 16. If _maxEntries is not within this range, it will be adjusted up, or down, whichever is appropriate (an ArgumentOutOfRangeException will not be thrown).

Delegates

  • public delegate void ClickedHandler(int number, string filename)
    • number - The MRU relative number of the entry that was clicked
    • filename - The filename of the entry that was clicked

Properties

  • ToolStripItemCollection MenuItems (ReadOnly)

    The ToolStripItemCollection where the MRU list is located.

  • int StartIndex (ReadOnly)

    The menu index of the first MRU entry.

  • int EndIndex (ReadOnly)

    The menu index immediately "after" the last MRU entry.

  • int NumEntries (ReadOnly)

    The current number of MRU entries.

  • int MaxEntries (Read/Write)

    The maximum number of MRU entries allowed. You can set the MaxEntries to the range of 4 through 16. If the new maximum entries is less than the current number of entries, the oldest entries will be discarded.

  • int MaxShortenPathLength (Read/Write)

    The maximum length of the path to allow in the menu, in characters. When setting, any value less than 16 will be changed to 16. The new length will have no effect on the current contents of the menu.

  • string RegistryKeyName (Read/Write)

    Set/Get the registry key name where the MRU list will be loaded/saved. No error checking is done to verify if the key is valid.

Static Methods

C#
static string FixupEntryname(int number, string entryname)

Adds the entry number prefix and mnemonic (for entries 0-9) to the filename. Note that the number is zero based, while the actually displayed numbering is one based.

  • number

    The MRU relative number to use for the prefix.

  • entryname

    The string to which to apply the prefix.

C#
static String ShortenPathname(string pathname, int maxLength)

If the pathname is longer than maxLength, it replaces consecutive path components by ellipsis. It will always preserve the root of the path and at least three characters of the filename, which may cause the result to be longer than maxLength.

  • pathname

    The pathname to check and, possibly, shorten. The pathname should be fully resolved and should start with a drive letter or be a UNC path.

  • maxLength

    The maximum length, in characters, of the resulting path.

Methods

C#
int FindFilenameNumber(string filename)

Find the MRU relative number of the entry which matches filename. This match must be exact except for case sensitivity.

Returns -1 if a matching entry cannot be found.

  • filename

    The filename to find.

C#
int FindFilenameMenuIndex(string filename)

Find the menu index of the entry which matches filename. This match must be exact except for case sensitivity.

Returns -1 if a matching entry cannot be found.

  • filename

    The filename to find.

C#
int GetMenuIndex(int number)

Returns the menu index of the entry at the MRU relative number.

  • number

    The MRU relative number of the menu index.

C#
string GetFileAt(int number)

Returns the filename stored at the MRU relative number.

  • number

    The MRU relative number of the filename to remove. Note that this number is zero based, while the display is one based.

C#
string[] GetFiles()

Returns the list of files in the MRU list. The most recent file will be at index zero.

C#
void SetFiles(String[] filenames)

Replaces the MRU entries with filenames. (Functionally equivalent to: RemoveAll(); AddFiles(filenames);.) The filenames will be added such that the first string in the array will be the topmost on the MRU list. (In other words, the entries will be added from the end to the beginning.)

  • filenames

    The list of filenames to set.

C#
void SetFirstFile(int number)

Makes the specified entry the first on the MRU list.

  • number

    The MRU relative number of the entry to make the first. Note that this number is zero based, while the display is one based.

C#
void AddFiles(String[] filenames)

Adds filenames to the list of files. The filenames will be added such that the first string in the array will be the topmost on the MRU list. (In other words, the entries will be added from the end to the beginning.)

  • filenames

    The list of filenames to add.

C#
void AddFile(String filename)

Adds a filename to the MRU list. If the entry exists, it will be moved to the first position. Otherwise it will be added in the first position. If the number of entries exceeds maxEntries, the least recent file (the last one) will be removed.

Before being added, the filename will first be resolved to an absolute path. The result will then be passed to the ShortenPathname function. Despite the absolute path resolution, it is possible for multiple entries to refer to the same physical file.

  • filename

    The filename to add.

C#
void AddFile(String filename, String entryname)

Adds entryname to the MRU list and stores filename with that entry. If the entry exists, it will be moved to the first position. Otherwise it will be added in the first position. If the number of entries exceeds maxEntries, the least recent file (the last one) will be removed. Note that filename will be used to determine whether an entry exists. Thus it is possible to have two identical entries in the MRU list, even though the associated filenames are different.

  • filename

    The filename to add.

  • entryname

    The text to use in the MRU menu.

C#
void RemoveFile(int number)

Removes the entry at the MRU relative number.

  • number

    The MRU relative number of the entry to remove. Note that this number is zero based, while the display is one based.

C#
void RemoveFile(String filename)

Removes the entry associated with filename.

  • filename

    The filename to remove.

C#
void RemoveAll()

Removes all MRU entries.

C#
void LoadFromRegistry(String keyName)

Sets the registry key and loads the entries located at that key.

  • keyName

    The registry key from which to restore the MRU entries.

C#
void LoadFromRegistry()

Loads the entries located at the stored key.

C#
void SaveToRegistry(String keyName)

Sets the registry key and saves the entries to that key.

  • keyName

    The registry key where the MRU entries will be stored.

C#
void SaveToRegistry()

Saves the entries located at the stored key.

Changes

24 September 2005

  • Posted on CodeProject

16 October 2007

  • Fixed a bug in FixupPrefixes where the first character was being stripped (thanks to shostakovich55 for that catch and his elegant solution)
  • Constructor now calls property to set MaxEntries which does range checking

5 November 2009

  • Fixed the RemoveAll bug first observed by Mark Rice, ignored by me, and then verified by hgy. My apologies to all. hgy's fix was pasted in and tested. Good work and thank you!

License

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


Written By
Software Developer (Senior)
United States United States
Joe is one of those software engineers with a film degree. His first paid programming job (you think film is a good way to make a living?) was writing games for Apple II's using 6502 assembly. He soon moved to 80x86 assembly, C, C++ (for a long time), C# and then back to C++ with occasional dabbling in C#, Python and other vile languages.

He first wrote software for Windows 3.0 in 1990. Save for some work in Linux, DOS and a mercifully brief foray into OS/2, he has concentrated on designing and writing software for all versions and types of Windows except RT.

Comments and Discussions

 
GeneralVB.Net version? Pin
mraviator9-Jun-11 6:57
mraviator9-Jun-11 6:57 
GeneralRe: VB.Net version? Pin
Joe Woodbury9-Jun-11 8:26
professionalJoe Woodbury9-Jun-11 8:26 

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

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