NCS Logo - Click for home page Northstar Developer Center
Platforms
All Platforms
.NET Framework (1.x - 4.x)
Active Server Pages
ASP.NET
C#
SQL Server
VB.NET
Visual Basic

Keywords
.NET Data Types
.NET E-mail
.NET Events
.NET Functions
.NET Object Programming
.NET System.Configuration
.NET System.Diagnostics
.NET System.IO
.NET System.Net
.NET System.Net.Sockets
Active Data Objects
ASP Architecture
ASP Black Belt
ASP Built-in Functions
ASP Built-in Objects
ASP Debugging
ASP Performance
ASP Security
ASP Syntax
ASP.NET Authentication
ASP.NET Controls
ASP.NET Data Access
ASP.NET Features
ASP.NET Master Pages
ASP.NET Page Events
ASP.NET Security
ASP.NET ViewState
Atom
Certifications
COM, DCOM, COM+
Data Access
E-Mail
Errors
Exporting Data
HTML Tips
IIS
Object-Oriented Programming
RSS
SQL
Uncategorized ASP Tips
VB API Programming
VB Forms
VB Syntax
XML

Book Support
Visual Basic 6 Bible
ASP Bible
ASP Weekend Crash Course
ASP.NET At Work
Creating Web Services

Building a Most-Recently Used File List

Written by Eric Smith, Northstar Computer Systems LLC

One common feature in applications like Word and Excel is the Most Recently Used (MRU) list at the bottom of the File menu. This list shows the last four (typically) files that had been opened in the application. This is easy to build using the standard VB menu editor and a bit of code.

In a typical File menu, there is a separator bar between the Exit choice at the bottom and other choices at the top. If you have a MRU list, there is another separator before those items. In your File menu, add a choice called mnuFileMRU with an index of zero. The Caption for this menu item should be a single dash, which will create a separator bar. Since we don’t want to show the separator if there are no files in the list, mark this choice as invisible at startup.

Once you’ve got that done, you can add this code to your form:

Private Sub AddToMRUList(strFilename As String)
   Dim i As Integer
   
   mnuFileMRU(0).Visible = True
   If m_intMRU < 4 Then
      Load mnuFileMRU(m_intMRU + 1)
      m_intMRU = m_intMRU + 1
   End If
   For i = m_intMRU - 1 To 1 Step -1
      mnuFileMRU(i + 1).Caption = "&" & (i + 1) & " " _
         & Mid(mnuFileMRU(i).Caption, InStr(mnuFileMRU(i).Caption, " ") + 1)
   Next i
   With mnuFileMRU(1)
      .Caption = "&1 " & strFilename
      .Visible = True
   End With
End Sub
You’ll also need to add this variable declaration to the declarations section of the form. The m prefix indicates a module-level variable.
Private m_intMRU As Integer
Since we know we will either be adding an item or changing an item in the MRU list, we show the separator bar, currently named mnuFileMRU(0). The code first determines how many items are in the MRU list. Since we can’t use the UBound function on a control array, we keep a separate variable (m_intMRU) with the current number of files. If we are less than four, we use the Load statement to create a new menu choice with a new index value, which is automatically added after the choice with index zero.

We then have to shuffle all the names down; that is, #1 becomes #2, #2 becomes #3, #3 becomes #4, and #4 is dropped if we already have four. Each menu item will look like this:

&1 Filenamegoeshere.txt
The ampersand causes the 1 to be underlined in the menu choice. When we shuffle the choices down, we have to remove the ampersand and number before putting the new number on. Once the old choices are shuffled down, we store the new one in spot #1.

This subroutine is called whenever I open a file in the text editor that I wrote. If you want to add an extra feature, have the code check to see if the file you selected is already in the list. If so, move it to the top and shuffle the rest down to fill the empty spot.

Keywords: [ VB Forms ]

Publication Date: 7/17/2000, Last Update: 2/13/2010