Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake an MRU list
DescriptionThis example shows how to make an MRU list in Visual Basic 6. The program saves and restores the MRU list in the registry.
KeywordsMRU, most recently used files, MRU list
CategoriesSoftware Engineering, Controls, Tips and Tricks
 
The m_FileTitles and m_FileNames collections hold the titles (without directory path) and names of the files in the MRU list. Subroutine AddToMRUList adds a file to the top of the list, removing duplicates.

Subroutine ShowMRUList uses the form's MRU list menu entries to display the list.

 
Private m_FileTitles As Collection
Private m_FileNames As Collection

' Add this file to the MRU list.
Private Sub AddToMRUList(ByVal file_title As String, ByVal _
    file_name As String)
Dim i As Integer

    ' Add the entry at the top of the list.
    If m_FileTitles.Count = 0 Then
        m_FileTitles.Add file_title
        m_FileNames.Add file_name
    Else
        m_FileTitles.Add file_title, , 1
        m_FileNames.Add file_name, , 1
    End If

    ' Remove any duplicates.
    For i = m_FileTitles.Count To 2 Step -1
        If m_FileNames(i) = file_name Then
            m_FileTitles.Remove i
            m_FileNames.Remove i
        End If
    Next i

    ' Remove entries beyond four.
    Do While m_FileTitles.Count > 4
        m_FileTitles.Remove 5
        m_FileNames.Remove 5
    Loop

    ' Redisplay the list.
    ShowMRUList

    ' Save the MRU list.
    SaveMRUList
End Sub

' Display the MRU list.
Private Sub ShowMRUList()
Dim i As Integer

    For i = 1 To m_FileTitles.Count
        mnuFileMRU(i).Caption = "&" & Format$(i) & " " & _
            m_FileTitles(i)
        mnuFileMRU(i).Visible = True
    Next i

    ' If any file is visible, display the MRU separator.
    mnuFileMRUSep.Visible = mnuFileMRU(1).Visible
End Sub
 
Subroutine SaveMRUList saves the MRU list into the Registry. It deletes any Registry entries for files that are not in use. For example, if the MRU list contains only 2 files, then this routine deletes the entries for files 3 and 4.

Note that the program saves the list every time it changes. It's really annoying if your program crashes and doesn't know about the last file you used.

Subroutine LoadMRUList reloads the list from the Registry.

 
' Save the MRU list.
Private Sub SaveMRUList()
Dim i As Integer

    ' Save the MRU files.
    For i = 1 To m_FileTitles.Count
        SaveSetting "MRUDemo", "MRU", "Title" & Format$(i), _
            m_FileTitles(i)
        SaveSetting "MRUDemo", "MRU", "File" & Format$(i), _
            m_FileNames(i)
    Next i

    ' Delete any old entries.
    On Error Resume Next
    For i = m_FileTitles.Count + 1 To 4
        DeleteSetting "MRUDemo", "MRU", "Title" & Format$(i)
        DeleteSetting "MRUDemo", "MRU", "File" & Format$(i)
    Next i
End Sub

' Load the MRU list.
Private Sub LoadMRUList()
Dim i As Integer
Dim txt As String

    Set m_FileTitles = New Collection
    Set m_FileNames = New Collection

    ' Get the MRU files.
    For i = 1 To 4
        txt = GetSetting("MRUDemo", "MRU", "Title" & _
            Format$(i), "")
        If Len(txt) = 0 Then Exit For

        m_FileTitles.Add txt
        m_FileNames.Add GetSetting("MRUDemo", "MRU", "File" _
            & Format$(i), "")
    Next i
End Sub
 
When the program starts, it calls subroutines LoadMRUList and ShowMRUList to load and display the MRU list.
 
Private Sub Form_Load()
    dlgFile.Flags = _
        cdlOFNFileMustExist Or _
        cdlOFNHideReadOnly Or _
        cdlOFNLongNames
    dlgFile.CancelError = True
    dlgFile.Filter = "Text Files (*.txt)|*.txt|All Files " & _
        "(*.*)|*.*"
    dlgFile.FilterIndex = 0

    LoadMRUList
    ShowMRUList
End Sub
 
Subroutine LoadFile loads a file and calls AddToMRUList to add the file to the list.
 
' Load the file and add it to the MRU list.
Private Sub LoadFile(ByVal file_title As String, ByVal _
    file_name As String)
Dim fnum As Integer

    On Error GoTo FileLoadError
    fnum = FreeFile
    Open file_name For Input As fnum
    txtFile.Text = Input$(LOF(fnum), #fnum)
    Close #fnum

    AddToMRUList file_title, file_name

    Caption = "Editor [" & file_title & "]"

    Exit Sub

FileLoadError:
    MsgBox "Error " & Format$(Err.Number) & _
        " reading file." & vbCrLf & Err.Description
    Exit Sub
End Sub
 
When you select an MRU list menu entry, the event handler calls subroutine LoadFile to reload the file. This also moves the file to the top of the MRU list.

If you use the File menu's Open command to open a file, the event handler calls subroutine LoadFile to load the file and add it to the MRU list.

 
' Open the selected file.
Private Sub mnuFileMRU_Click(Index As Integer)
    LoadFile m_FileTitles(Index), m_FileNames(Index)
End Sub

Private Sub mnuFileOpen_Click()
    On Error Resume Next
    dlgFile.ShowOpen

    If Err.Number = cdlCancel Then
        Exit Sub
    ElseIf Err.Number <> 0 Then
        MsgBox "Error " & Format$(Err.Number) & _
            " selecting file." & vbCrLf & Err.Description
        Exit Sub
    End If

    LoadFile dlgFile.FileTitle, dlgFile.FileName
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated