Solved

Saving dynamic menus between runs

Posted on 2000-04-16
4
162 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 28

Accepted Solution

by:
AzraSound earned 50 total points
ID: 2720788
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
ID: 2720791
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
ID: 2725206
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
ID: 2725230
I am sure you will   =)
glad I could be of service...take care
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

724 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