Class: MRU in Category Microsoft Visual Basic 6 : Menus from Total Visual SourceBook

Maintain a "Most Recently Used" menu items on a Visual Basic 6 (VB6) form with the use of Windows Registry entries.

This class is commonly used to store the "Most Recently Used" file list for menu items on a form, but the list of items does not have to be a list of files. It could be used for any unique items you wish to store and display on a form's menu.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the CMRU class.
MaxMRUItems Property Get the number of visible menu items, not counting the separator.
MenuItem Property Get the specified menu item. All entries from 0 to the value of MaxMenuItems must be assigned a particular menu on your form. The value 0 is used for the "separator" menu item.
Prefix Property Get the prefix string currently used for the MRU entries in the registry (defaults to MRU).
RegistryPath Property Get the current registry path string used for the MRU entries (defaults to SOFTWARE\ plus the value of App.Title).
RootKey Property Get the RootKey of the registry location used for the MRU entries in the registry (defaults to mrrkHKeyLocalMachine, or the "HKEY_LOCAL_MACHINE" hive of the registry).
SubKey Property Get the current SubKey of the registry location used for the MRU entries in the registry. If not specified, or set to "", + the MRU settings are stored in the root of the key indicated by the RegistryPath property.
TagPrefix Property Get the current Tag Prefix used for the MRU tag entries in the registry (defaults to "MRUTAG").
Class_Initialize Initialize Set initial values to defaults which may be overridden with property settings.
Class_Terminate Terminate Clean up local static storage.
GetMRUList Method Retrieve the current MRU entries from the Windows registry, and update the captions for the form's MRU Menu Items. Use this to update your form's menus with the latest values stored in the MRU portion of the registry maintained by the SetMRUList method. It is not necessary to call this method after SetMRUList, since the SetMRUList method automatically retrieves the latest information.
SetMRUList Method Add the specified item to the top of the MRU list or clear the MRU list.
RegistryGetKeyString Private Get a string value from the system registry.
RegistrySetKeyValue Private Set a key value in the registry, creating it if it doesn't already exist.
' Example of the CMRU class
'
' To try this example, do the following:
' 1. Create a new form
' 2. Create the menu structure consisting of a top-level menu called 'mnuFile'.
' 3. Underneath this menu, create menu items called 'mnuFileOpen' and 'mnuFileSave'
' 4. Following mnuFileSave add a menu control array called 'mnuFileMRU' with 5 items, indexes 0 through 4.
'    The caption for the first mnuFileMRU item should be set to "-", which indicates that it is the divider item.
' 5. The complete menu structure should look something like this in the VBA/VB6 Tools, Menu Editor Dialog:
'
'    Caption         Menu Control Name
'    -----------------------------------
'    &File           mnuFile
'    ....&Open       mnuFileOpen
'    ....&Save       mnuFileSave
'    ....-           mnuFileMRU(0)
'    ....            mnuFileMRU(1)
'    ....            mnuFileMRU(2)
'    ....            mnuFileMRU(3)
'    ....            mnuFileMRU(4)
'
' 6. Add a FileOpen common file dialog control to the form, and name it "CommonDialog1"
' 7. Paste all the code from this example to the new form's module
' 8. Run the form. Try opening and saving files with the File Open and File Save menu,
'    and watching what happens to the Most Recently Used items on the File Menu.

' Declarations section code
Private mMRU As CMRU

Private Sub Form_Load()
  Set mMRU = New CMRU

  With mMRU
    .RootKey = mrrkHKeyLocalMachine
    .RegistryPath = "SOFTWARE\FMS\TestMRU"
    .SubKey = "MRU Entries"
    .Prefix = "MRU"
    .TagPrefix = "MRUTag"

    ' Assign the actual form's menu items to the corresponding properties of the CMRU object
    Set .MenuItem(0) = mnuFileMRU(0)
    Set .MenuItem(1) = mnuFileMRU(1)
    Set .MenuItem(2) = mnuFileMRU(2)
    Set .MenuItem(3) = mnuFileMRU(3)
    Set .MenuItem(4) = mnuFileMRU(4)

    ' Get the most recently saved list of files and update the menus
    .GetMRUList

  End With

End Sub

Private Sub mnuFileMRU_Click(Index As Integer)
  Dim strFile As String
  Dim strTag As String

  ' display the value stored in the caption and tag of the menu item
  strFile = mid$(mnuFileMRU(Index).Caption, 5)
  strTag = mnuFileMRU(Index).Tag

  MsgBox "Caption: " & strFile & vbCrLf & "Tag: " & strTag

  ' Move the selected menu item to the top of the list
  mMRU.SetMRUList strFile, strTag

End Sub

Private Sub mnuFileOpen_Click()
  ' Simulate an application opening a file, and updating the MRU list
  With CommonDialog1
    .DialogTitle = "Load File"
    .ShowOpen
    If .FileName <> "" Then
  ' Add the new file to the top of the list if it does not already exist.
  ' Otherwise, move the file from its current position to the top of the list.
  ' Store the length of the file in the .tag property of the menu item
      mMRU.SetMRUList .FileName, "File length: " & FileLen(.FileName)

    End If

  End With

End Sub

Private Sub mnuFileSave_Click()
  ' Simulate an application saving a new file, and updating the MRU list
  With CommonDialog1
    .DialogTitle = "Save File"
    .ShowOpen
    If .FileName <> "" Then
      mMRU.SetMRUList .FileName, "File length: " & FileLen(.FileName)
    End If

  End With

End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!


View all FMS products for Microsoft Access All Our Microsoft Access Products

Reviews

Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info

Question

 

 

Free Product Catalog from FMS