• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 171
  • Last Modified:

Saving dynamic menus between runs

I've created dynamic menus that display the path and filenames of selected .wav and .avi files when users wish to add their own files to my application.  This I've done via the menu editor and some coding in VB6.0.  Yet, I want the application to save these menus and display them each time the application is run, much like newly opened files are remembered and displayed in Microsoft Word.  How do I accomplish this?
0
amadei2000
Asked:
amadei2000
  • 3
1 Solution
 
AzraSoundCommented:
Const RECENT_FILE_KEY = "RecentFiles"
Private Sub MDIForm_Load()
    GetRecentFiles
End Sub

Private Sub Form_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
        End If
    Next i
    OnRecentFilesList = False
End Function

Private Sub mnuFileOpen_Click()
    Static intval As Integer
    intval = intval + 1
    '// add dummy item
    UpdateFileMenu "C:\test" & intval & ".txt"
End Sub
0
 
AzraSoundCommented:
to use this add this to a menu:

Captions:

&File
.....&Open
.....-
.....RecentFile1
.....RecentFile2
.....RecentFile3
.....RecentFile4 mnuFile

Names:


mnuFileOpen
mnuRecentFileSep
mnuRecentFile  (Index 1)
mnuRecentFile  (Index 2)
mnuRecentFile  (Index 3)
mnuRecentFile  (Index 4)


0
 
amadei2000Author Commented:
It didn't occur to me to access the Recent Files stored in the registry....  I am new to VB and programming in general... Your guidance was far more than I had expected!  Your code is well documented and easy to follow...  Thank you so much for your insight!  Perhaps some day I will also be able to help a lost soul find his/her way through the VB jungle....  
0
 
AzraSoundCommented:
I am sure you will   =)
glad I could be of service...take care
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now