The new vbAccelerator Site - more VB and .NET Code and Controls
Source Code
3 Code Libraries &nbsp

A drop-in Most Recently Used (MRU) File List class

A quick and reliable way of providing a Most-Recently Used file list in your application


[Most Recently Used File List Example]

Download the cMRUFileList Class (2kb) Download the cMRUFileList Demo Application (17kb)

cMRUFileList gives you a quick and reliable way to provide a Most-Recently Used (MRU) file list in your application. I wrote this because I found I'd tried rewritten the same code and had the same problems trying to get it to work once too many!

The class is simple really, but at least I can be sure it works. It supports:

  • Loading and saving the MRU list to the Registry (via my Registry class).
  • Customising the number of MRU entries to hold.
Here is how you use it:

To Load the Stored MRU List
My class assumes that you will want to use the registry to hold other application information. It expects to receive a reference to a cRegistry class which is already pointing to the key and section where your application is storing its persistent information:

&nbsp &nbsp Dim cR As New cRegistry
&nbsp &nbsp cR.ClassKey = HKEY_CURRENT_USER
&nbsp &nbsp cR.SectionKey = "Software\vbaccelerator\MRUDemo"
&nbsp &nbsp m_cMRU.Load cR
&nbsp &nbsp m_cMRU.MaxFileCount = 4
&nbsp &nbsp pDisplayMRU

To Display The MRU List
There are various ways to use menus to display the information. The simplest way is to create sufficient menu entries in a menu array and set them to invisible in the menu editor. Then you do not have to worry about loading new menu items, you just loop through the entries in the MRU list and make the menu item visible/invisible as appropriate:

Private Sub pDisplayMRU
Dim iFile As Long
&nbsp &nbsp ' Here I am assuming the MRU is held in a menu array
&nbsp &nbsp ' called mnuFile, to start at Index 2:
&nbsp &nbsp For iFile = 1 To m_cMRU.FileCount
&nbsp &nbsp &nbsp &nbsp
&nbsp &nbsp &nbsp &nbsp If (m_cMRU.FileExists(iFile)) Then
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp If iFile = 1 Then mnuFile(iFile + 1).Checked = True
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp mnuFile(iFile + 1).Visible = True
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp mnuFile(iFile + 1).Caption = m_cMRU.MenuCaption(iFile)
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp mnuFile(iFile + 1).Tag = CStr(iFile)
&nbsp &nbsp &nbsp &nbsp End If
&nbsp &nbsp Next iFile
&nbsp &nbsp mnuFile(2 + m_cMRU.FileCount).Visible = (m_cMRU.FileCount > 0)

End Sub

Whenever a new file is opened, or choosen from the MRU List
Call the m_cMRU.AddFile method with the chosen file as the parameter and then redisplay the MRU list. The AddFile method will check whether the file in question is already in the list, if it is it will simply reorder it to the top of the list.

To Save the Stored MRU List
This works in a very similar way to the Load method:

&nbsp &nbsp Dim cR As New cRegistry
&nbsp &nbsp cR.ClassKey = HKEY_CURRENT_USER
&nbsp &nbsp cR.SectionKey = "Software\vbaccelerator\MRUDemo"
&nbsp &nbsp m_cMRU.Save cR

TopBack to top
Source Code - What We're About!Back to Source Code


AboutContributeSend FeedbackPrivacy

Copyright 1998-1999, Steve McMahon ( All Rights Reserved.
Last updated: 15 August 1999