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