Solved

Saving dynamic menus between runs

Posted on 2000-04-16
4
160 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel - Save a copy of work book 13 91
How to set the sa password in a vb6 code for sql connection 9 63
vbModal 12 66
adding "ungroup sheets" to existing vbs code 5 56
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

733 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