Library tutorials & articles

Menus

Recent File List

You will have noticed that almost all applications have a list of recently used files on their menus.

We now have a cHistory class, which you can easily add to your own project with only a few lines of code. Alternatively, you can take a look at the code below...

This is actually quite easy to do in visual basic, once you know how. First, create a new project, and add the following menu items:

Structure & Caption Name Index
&File
....&Open
....-
....RecentFile1
....RecentFile2
....RecentFile3
....RecentFile4
mnuFile
mnuFileOpen
mnuRecentFileSep
mnuRecentFile
mnuRecentFile
mnuRecentFile
mnuRecentFile



1
2
3
4

You can add some more RecentFile items if you wish. Simply increment the Index property. The code shown below will work with any number of items.

Option Explicit
Const RECENT_FILE_KEY = "RecentFiles"
Private Sub MDIForm_Load()
    GetRecentFiles
End Sub

'// RecentFile menu item event
Private Sub mnuRecentFile_Click(Index As Integer)
    '// load file etc
    MsgBox "LoadFile: " & mnuRecentFile(Index).Tag
    '// move clicked item to the top
    UpdateFileMenu (mnuRecentFile(Index).Tag)
End Sub

'// gets all the recent files stored in the registry
Public Sub GetRecentFiles()
    Dim i        As Integer
    Dim varFiles As Variant ' Variable to store the returned array.
    Dim strTitle As String
    Dim strPath  As String
    Dim Item     As Menu
    '// App.Title and RECENT_FILE_KEY are constants defined in this module.
    '// hide all the items
    On Error Resume Next
    For Each Item In mnuRecentFile
        Item.Visible = False
    Next
    If GetSetting(App.Title, RECENT_FILE_KEY, "RecentFile1") = Empty Then
        '// no files on list, hide seperator
        mnuRecentFileSep.Visible = False
        Exit Sub
    Else
        '// load items
        For i = 1 To mnuRecentFile.Count
            '// get path
            strPath = GetSetting(App.Title, RECENT_FILE_KEY, "RecentFile" & i)
            '// extract title
            strTitle = Right$(strPath, Len(strPath) - InStrRev(strPath, ""))
            '// set caption to number & title only
            '// you could replace strTitle with strPath
            '// if you wanted the full path to be displayed
            mnuRecentFile(i).Caption = "&" & i & " " & strTitle
            '// set tag to path
            mnuRecentFile(i).Tag = strPath
           
            If mnuRecentFile(i).Caption = "&" & i & " " Then
                '// no more items, exit for
                Exit For
            End If
            '// show item
            mnuRecentFile(i).Visible = True
        Next
        '// show seperator
        mnuRecentFileSep.Visible = True
    End If
End Sub

'// adds a new file to the top of the list
Private Sub WriteRecentFiles(strFileName As String)
    Dim i       As Integer
    Dim strFile As String
    Dim strKey  As String
    '// Move all items down one
    '// start from one up from bottom, so that bottom
    '// item is overwritten
    For i = mnuRecentFile().Count - 1 To 1 Step -1
        '// get path
        strFile = mnuRecentFile(i).Tag
        If strFile <> "" Then
            '// save item in new location
            strKey = "RecentFile" & (i + 1)
            SaveSetting App.Title, RECENT_FILE_KEY, strKey, strFile
        End If
    Next i
    '// Write the specified file to first recent file.
    SaveSetting App.Title, RECENT_FILE_KEY, "RecentFile1", strFileName
End Sub

'// This sub moves the specified file
'// to the top of the list
'// from wherever it is
Private Sub MoveRecentFiles(strFileName As String)
    Dim intLocation  As Integer
    Dim i            As Integer
    Dim strFile      As String
    Dim strKey       As String
    '// Get location of specified file
    For intLocation = 1 To mnuRecentFile.Count
        strKey = "RecentFile" & intLocation
        strFile = GetSetting(App.Title, RECENT_FILE_KEY, strKey)
        If strFile = strFileName Then
            '// found item
            Exit For
        End If
    Next
    '// Move all items down upto location of strFileName
    '// start from item before the loc of strFileName
    For i = intLocation - 1 To 1 Step -1
        '// get saved path
        strFile = GetSetting(App.Title, RECENT_FILE_KEY, "RecentFile" & i)
        If strFile <> "" Then
            '// move item down one
            strKey = "RecentFile" & (i + 1)
            '// save new location
            SaveSetting App.Title, RECENT_FILE_KEY, strKey, strFile
        End If
    Next i
    '// save deleted item at top
    strKey = "RecentFile1"
    SaveSetting App.Title, RECENT_FILE_KEY, strKey, strFileName
End Sub

'// Updates the file menu:
'// either adds a new item at the top for new file
'// or moves the existing item to the top
Sub UpdateFileMenu(strFileName)
    '// Check if the open filename is already in the File menu control array.
    If OnRecentFilesList(strFileName) Then
        '// move the existing item to the top
        MoveRecentFiles (strFileName)
    Else
        '// add a new item at the top for new file
        WriteRecentFiles (strFileName)
    End If
    '// Update the list of the most recently opened files in the File menu control array.
    GetRecentFiles
End Sub

'// Checks to see if item is on the File list already
Private Function OnRecentFilesList(FileName) As Integer
    Dim i As Integer '// Counter variable.
    For i = 1 To mnuRecentFile.Count
        If mnuRecentFile(i).Tag = FileName Then
            OnRecentFilesList = True
            Exit Function
  &n

Comments

  1. 28 Apr 2003 at 04:42

    In this tutorial i dosent tell you the code for the menu. and im also very new to vb so if someone could tell me what i need to do for the code that would be great.


  2. 17 Nov 2002 at 19:54

    I have made a menu with1 1 Item (about) set it to enable - unchecked visable
    made a click event(msg box)


    then in my form mouse up event I put

    Code:

    Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbRightButton Then
       ' Set the flags
       vFlags = vbPopupMenuRightButton
       ' Display the menu
       PopupMenu about, vFlags
       End If
    End Sub
     I get compile error variable not defined   with vFlags?
    do i need to dim vFlags = "somthing " in the  Option Explicit section of the code?

  3. 20 May 2002 at 12:39

    sorry 'bout that... there was an old style link lurking in there... I have corrected the link on


    http://www.developerfusion.com/show/15/10/, which now points to


    http://www.developerfusion.com/show/49/4/ (menu status subclassing)


    not sure about changing the colour of your menu, other than drawing it yourself, or using one of the http://vbaccelerator.com/ controls

  4. 20 May 2002 at 12:11

    i click view an example and no example???
    i really want to change the look of my menu,change color or nything? ny ideas?

  5. 01 Jan 1999 at 00:00

    This thread is for discussions of Menus.

Leave a comment

Sign in or Join us (it's free).