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

MRU Menu Class Revisited

, 19 Jun 2010
Rate this:
Please Sign up or sign in to vote.
Class for automating the handling of Most Recently Used files in a MenuStrip.

Screen1.png

Introduction

The purpose of the MRU menu class is to automate the handling of Most Recently Used files in an application. It has been designed so that you can drop the class into an application that is using a MenuStrip, tell it when you open a new file, and let it do the rest.

Background

Six years ago, I wrote an article on an MRU menu class for .NET Framework 1.0 (MRU Menu Class). Two and a half frameworks later, I thought that I would redo the article and add some enhancements.

How to Use the MRU Menu Class

Initializing the Class

The MRU class is very simple to use. First, we need to declare a new MRUlist object to use on our form:

Public WithEvents mruList As CMRmedia.MRUMenu

Now we are ready to initialize the class in the form's Load event:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'Initialize MRU list and associate the MenuItem to use as the Recent Files menu
    mruList = New CMRmedia.MRUMenu(RecentToolStripMenuItem, Me)

    mruList.FileName = "x:\Path\To\My\File.xml"
    mruList.MaxItems = 3
    mruList.Width = 50
    mruList.ShowClearRecent() = True
    mruList.Validate = False
    mruList.StoreRelativePaths = True

    'Loads the MRU list for persisted file if it exists
    mruList.Load()
End Sub
  • FileName
  • This is the path to the XML file that will store the MRU list for persistence.

  • MaxItems
  • This is the maximum number of items that will be displayed in the MRU menu.

  • Width
  • This is the width in characters of the MRU menu items.

  • ShowClearRecent
  • This determines if the MRU menu will display a Clear List item for erasing the MRU list.

  • Validate
  • When the MRU class loads the list from a file, it will remove the files that no longer exist. You can use this option to remove this functionality.

  • StoreRelativePaths
  • If you are running your application from removable media, you can turn on the option to store the file paths relative to the application directory by enabling this option.

Adding Files to the List

When your application has handled a new file, you can add the file to the MRU list by simply calling the following code:

mruList.AddItem("Full File Path as string")

How do I Handle an MRU Item Being Clicked

So what happens when you click on one of the items in the MRU menu? Well, if it is one of the MRU items, a click event is fired by the class, which you can capture with the following:

Private Sub MruFileClicked(ByVal mruPath As String) Handles mruList.mruItemClick
    MsgBox(mruPath)
End Sub

The event returns the clicked file's full path in the mruPath variable, so you can do whatever you need with the file.

If Clear List is clicked, the MRU class clears out any files that are currently stored.

And finally, if the More option is clicked, the following window is opened displaying the full list of MRU items available since last being cleared.

Screen2.png

Error Handling

So what happens if the class can't perform an action? Well, it fires an error event that you can capture like this:

Private Sub MruError(ByVal ex As MruException) Handles mruList.MruError
    Dim str As String = "Error: " & ex.ErrorType.ToString & vbCr
    str += "Message: " & ex.message & vbCr
    str += "Inner Exception: " & ex.innerException
    MsgBox(str, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "MRU Menu Error")
End Sub

Points of Interest

It was very interesting to compare my methods from six years ago to today. All in all, this may not be the absolute best solution, but I have used this in many of my applications as it has evolved over the years. The sample code includes documentation to help beginners understand what is happening. I hope you find it helpful.

History

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

chad_ross
Product Manager
Canada Canada
No Biography provided

Comments and Discussions

 
GeneralMy vote of 4 PinmemberMalic the Mad23-Jul-12 9:16 
GeneralI love this code it's great and just what I was looking for ... almost. [modified] PinmemberMember 918432629-Jun-12 5:59 
Generalvery good work Pinmembersandra From Paris8-Sep-10 10:00 
GeneralMy vote of 3 PinmemberAlex Essilfie20-Jul-10 1:06 
GeneralGood work. PinmvpMd. Marufuzzaman11-Jul-10 22:40 
Generalok aticle PinmemberDonsw22-Jun-10 16:41 
GeneralRe: ok aticle Pinmemberbrgabriel@sspx.org20-Dec-11 16:19 
Well, I thank you for the code! However, this document seems more like a code showcase than a tutorial, which is fine if that is what you intended. I was able to study it and plug it into my project without too much work, and that I appreciate.

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 | Mobile
Web04 | 2.8.140827.1 | Last Updated 19 Jun 2010
Article Copyright 2010 by chad_ross
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid