![[Most Recently Used File List Example]](/images/filemru.gif)
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:
    Dim cR As New cRegistry
    cR.ClassKey = HKEY_CURRENT_USER
    cR.SectionKey = "Software\vbaccelerator\MRUDemo"
    m_cMRU.Load cR
    m_cMRU.MaxFileCount = 4
    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
    ' Here I am assuming the MRU is held in a menu array
    ' called mnuFile, to start at Index 2:
    For iFile = 1 To m_cMRU.FileCount
       
        If (m_cMRU.FileExists(iFile)) Then
            If iFile = 1 Then mnuFile(iFile + 1).Checked = True
            mnuFile(iFile + 1).Visible = True
            mnuFile(iFile + 1).Caption = m_cMRU.MenuCaption(iFile)
            mnuFile(iFile + 1).Tag = CStr(iFile)
        End If
    Next iFile
    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:
    Dim cR As New cRegistry
    cR.ClassKey = HKEY_CURRENT_USER
    cR.SectionKey = "Software\vbaccelerator\MRUDemo"
    m_cMRU.Save cR
Back to top
Back to Source Code