![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
Special Menus Many applications have special needs that can be met only with nonstandard menus. For example, word processing applications have many commands that change the formatting of text. For that reason, it makes sense to create a nonstandard Format menu rather than cramming these commands into an existing menu. These custom menus are usually placed to the right of all of the standard menus except the Help menu. The Help menu should always be the rightmost menu. Some applications include nonstandard commands in relevant standard menus. A word processing application could place a command to capitalize all of the words in a paragraph in the Edit menu because this command is related to editing. An application, however, should not include unrelated commands in a standard menu. It would be confusing to place this same command in the File or Help menu. If a group of commands does not fit within a standard menu, the application should create a new menu where the command will make more sense. Providing Recent File ListsMany applications present a list of the most recently loaded files in the File menu. Figure 1.14 shows a File menu with four recently loaded files listed above the Exit command. If the user selects one of these files, the program loads it. Displaying a recent file list is fairly easy in Visual Basic. ExpenseReporter uses several subroutines to manage its recent file list. UpdateRecentFileMenu displays the recent file names in File menu items. SaveRecentFiles and LoadRecentFiles save and load the recent file list from the system Registry. This allows ExpenseReporter to maintain the recent file list when the user quits and restarts the application. Subroutine AddRecentFile adds a new file to the recent file list when it is created or opened. RemoveRecentFile removes a file from the list if the file cannot be found. These routines, and the menu items that display the list, are described in the following sections. File List Menu ItemsSome applications place the recent file list below the Exit command. Most, however, place the list as shown in Figure 1.14immediately above Exit with separators between the list and the other commands in the File menu. Most applications save the four most recent file names though a few, including Microsoft Word, allow you to specify the number of files listed. Most applications also begin the recent file entries with a number (1, 2, 3, or 4) that is used as the files mnemonic. For example, in Figure 1.14 the first recent file entry begins with 1. The 1 is underlined to indicate that it is a mnemonic.
ExpenseReporter uses five menu items in its File menu to display the recent file list. The first four all have the name mnuFileList. They have indexes 1, 2, 3, and 4. Their captions do not matter because ExpenseReporter changes their captions at run time anyway. The fifth File menu item is a separator between the mnuFileList items and the Exit command. This separator is named mnuFileListSep. ExpenseReporter defines an array of strings named RecentFile in the main forms declarations section. This array holds the names of the four most recently accessed files. The four most recently loaded files. Dim RecentFile(1 To 4) As String UpdateRecentFileMenuSubroutine UpdateRecentFileMenu displays the File menus entries for the recent file names. First it checks to see if any of the file names are nonblank. If all of the names are blank, it sets the Visible property to false for the menu separator mnuFileListSep and all of the recent file menu entries mnuFileList. Otherwise, it makes the separator visible. UpdateRecentFileMenu then examines each of the recent file names. For any that are nonblank, it makes the corresponding menu entry visible and sets that entrys Caption property. It uses the entrys index as a mnemonic for the command. The subroutine hides any entries that correspond to blank file names.
Sub UpdateRecentFileMenu()
Dim i As Integer
If there are no recent file entries, hide
the menu entries and separator and exit.
If RecentFile(1) = Then
For i = 1 To 4
If mnuFileList(i).Visible Then _
mnuFileList(i).Visible = False
Next i
If mnuFileListSep.Visible Then _
mnuFileListSep.Visible = False
Exit Sub
End If
Since there is at least one entry, show the
recent file list separator.
If Not mnuFileListSep.Visible Then _
mnuFileListSep.Visible = True
Show the non-blank entries.
For i = 1 To 4
If RecentFile(i) <> Then
Show this non-blank entry.
mnuFileList(i).Caption = _
& & Format$(i) & & RecentFile(i)
If Not mnuFileList(i).Visible Then _
mnuFileList(i).Visible = True
Else
Hide this blank entry.
If mnuFileList(i).Visible Then _
mnuFileList(i).Visible = False
End If
Next i
End Sub
When the user selects one of the recent file menu entries, ExpenseReporter opens the corresponding file. The menu entries event handlers first make sure the currently loaded data is safe. If so, it calls the FileInput function to load the selected recent file.
Private Sub mnuFileList_Click(Index As Integer)
See if the data is safe.
If Not DataSafe(Do you want to save the changes?) _
Then Exit Sub
Load the recent file.
If FileInput(RecentFile(Index)) Then _
RemoveRecentFile Index
End Sub
SaveRecentFilesThe recent file list should be a permanent part of the users system. Each time the user starts the application, the File menu should list the four files that were most recently used the last time the application ran. In Windows 95 and Windows NT, this sort of permanent information is stored in the Windows Registry. Visual Basic provides some very simple functions for saving and retrieving Registry entries.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|