Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB10

MRU Menu Class Revisited

4.50/5 (16 votes)
19 Jun 2010GPL33 min read 32.3K   786  
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:

VB
Public WithEvents mruList As CMRmedia.MRUMenu

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

VB
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:

VB
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:

VB
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:

VB
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)