Click here to Skip to main content
15,879,096 members
Articles / Programming Languages / Visual Basic 10

MRU Menu Class Revisited

Rate me:
Please Sign up or sign in to vote.
4.50/5 (16 votes)
19 Jun 2010GPL33 min read 31.4K   783   21   7
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)


Written By
Product Manager
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
Malic the Mad23-Jul-12 9:16
Malic the Mad23-Jul-12 9:16 
GeneralI love this code it's great and just what I was looking for ... almost. Pin
Member 918432629-Jun-12 5:59
Member 918432629-Jun-12 5:59 
Generalvery good work Pin
sandra From Paris8-Sep-10 10:00
sandra From Paris8-Sep-10 10:00 
GeneralMy vote of 3 Pin
Alex Essilfie20-Jul-10 1:06
Alex Essilfie20-Jul-10 1:06 
GeneralGood work. Pin
Md. Marufuzzaman11-Jul-10 22:40
professionalMd. Marufuzzaman11-Jul-10 22:40 
Pretty good stuff...
Thanks
Md. Marufuzzaman


I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.

Generalok aticle Pin
Donsw22-Jun-10 16:41
Donsw22-Jun-10 16:41 
GeneralRe: ok aticle Pin
brother.gabriel20-Dec-11 16:19
brother.gabriel20-Dec-11 16:19 

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.