Add MRUs to Your Application - April 1998

Most new applications allow the user to open the most recently used (MRU) files. I recently needed to write MRU processing for an application. The results of the effort are described in this Tip of the Month.

Demo MRU Program

I wrote a demo program to illustrate adding MRU functionality to an MDI application. Some screen shots:

Image21.gif

There are 3 MRUs in this example. The user can ‘Open’ another file using file menu open, or can select one of the previously opened files. The demo allows the user to simulate opening any file. When opened, the file is added to the most recently used file menu and stored in the registry. If the user selects a file menu MRU item that file is ‘opened’ and it is moved to head of the list. The file is removed from the list if it no longer exists.

Registry Entries

Image22.gif

VB Modules and Classes

The following modules are needed to add MRU support to your application.

Module

Description

modMRU

Contains MRU handling logic. Public routines:
LoadMnuFileMRU – Load 9 file menu items.
BuildMRUCollection – Load or reload the MRUs collection by reading the MRUs from the registry.
BuildMnuFileMRU – Use the MRUs collection to build the file menu MRU captions.
AddOrUpdateMRUCollection – After the user opens a file, call this subroutine to add the file to the collection and to update the registry.

Private routine that needs to be changed:
SaveGetMRU – Reads or writes to the registry. You should modify the key in this function to point to your application’s registry entries. The demo uses:
"Software\The Bridge\MRU Sample"

clsMRU

A class module used to define an MRU object.

modRegistry

Routines RegRead and RegWrite are used to save and read MRUs from the registry. See: Letter 971202 Vol1 Num5 VB Sig RegRead.doc distributed in December 1997 for details.

The following modules are used by the demo program to ‘open’ a file.

CDialog CDialog is used to invoke the common dialog. Francesco Balena, fbalena@europa.pangeanet.it, wrote it. See: VB PJ December 1996, Page 22.
modCommonDialogConstants Contains the common dialog constants. Therefore the project does not need a reference to comdia32.oca.

Download VB Code

Click here to download the VB Source Code [4k].

Download As Word 97 Document

Click here to download this page as a zipped Word 97 Document [9k].

Previous Tip    Tip Index    Next Tip

Home

Updated 2000/10/29