Solved

Saving dynamic menus between runs

Posted on 2000-04-16
4
154 Views
Last Modified: 2010-05-02
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
Comment
Question by:amadei2000
  • 3
4 Comments
 
LVL 28

Accepted Solution

by:
AzraSound earned 50 total points
Comment Utility
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
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
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
 

Author Comment

by:amadei2000
Comment Utility
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
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
I am sure you will   =)
glad I could be of service...take care
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now