MRU LISTS

A Most Recently Used list (MRU list) is a series of menu items (usually at the bottom of an application’s File menu) that displays the files most recently accessed by the user. If the user clicks one of these menu items, the program reopens the corresponding file.

By convention, these menu items begin with the accelerator characters 1, 2, 3, and so forth. If you opened the File menu and pressed 2, for example, the program would reopen the second file in the MRU list.

When the user opens a new file or saves a file with a new name, that file is placed at the top of the list. Most applications display up to four items in the MRU list and, if the list ever contains more items, the oldest are removed.

Most applications remove a file from the MRU list if the application tries to open it and fails. For example, if the user selects an MRU menu item but the corresponding file has been removed from the system, the program removes the file’s menu item.

Building an MRU list isn’t too difficult in Visual Basic. The MruList example program, which is available for download on the book’s website, uses the MruList class to manage its MRU list. This class manages a menu that you want to use as an MRU list and updates the menu as the user opens and closes files. For example, if you configure the class to allow four MRU list entries and the user opens a fifth file, the class removes the oldest entry and adds the new one.

The class saves and restores the MRU list in the system’s Registry. When the user selects a file from the MRU list, the class raises an event so the main program’s code can open the corresponding file. The class also provides an Add method that the main program can use to add new files to the MRU list when the user opens a new file. Download the example and look at its code for more details.

The following code shows how the main MruList program uses the MruList class. This program is a simple text viewer that lets the user open and view files.

Public Class Form1
    Private WithEvents m_MruList As MruList
        
    ' Initialize the MRU list.
    Private Sub Form1_Load() Handles Me.Load
        m_MruList = New MruList("SdiMruList", mnuFile, 4)
    End Sub
        
    ' Let the user open a file.
    Private Sub mnuFileOpen_Click() Handles mnuFileOpen.Click
        If dlgOpen.ShowDialog() = Windows.Forms.DialogResult.OK Then
            OpenFile(dlgOpen.FileName)
        End If
    End Sub
        
    ' Open a file selected from the MRU list.
    Private Sub m_MruList_OpenFile(file_name As String) _
     Handles m_MruList.OpenFile
        OpenFile(file_name)
    End Sub
        
    ' Open a file and add it to the MRU list.
    Private Sub OpenFile(file_name As String)
        txtContents.Text = File.ReadAll(file_name)
        txtContents.Select(0, 0)
        m_MruList.Add(file_name)
        Me.Text = "[" & New FileInfo(file_name).Name & "]"
    End Sub
End Class

The program declares an MruList variable named m_MruList. It uses the WithEvents keyword so that it is easy to catch the object’s OpenFile event.

The form’s New event handler initializes the MruList object, passing it the application’s name, the File menu, and the number of items the MRU list should hold.

When the user selects the File menu’s Open command, the program displays an open file dialog box. If the user selects a file and clicks OK, the program calls subroutine OpenFile, passing it the name of the selected file.

If the user selects a file from the MRU list, the m_MruList_OpenFile event handler executes and calls subroutine OpenFile, passing it the name of the selected file.

Subroutine OpenFile loads the file’s contents into the txtContents TextBox control. It then calls the MruList object’s Add method, passing it the file’s name. It finishes by setting the form’s caption to the file’s name without its directory path.

You could easily convert the MruList class into a component so you could place it directly on the form. If you give the component ApplicationName, FileMenu, and MaxEntries properties, you can set those values at design time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.6.85