![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
Files Specified in the Command LineThere is one other way the user can specify a file to open. Using Windows Explorer the user may double-click on an expense report data file. The first time this happens, Explorer will ask the user what application to use to open the file. If ExpenseReporter has been compiled into an executable, the user can use it to open the file. At this point, the user can tell Explorer to associate .EXP files with the ExpenseReporter application so ExpenseReporter will automatically run whenever a .EXP file is opened in the future. When an application is started in this way, Windows Explorer passes the application the name of the file as a command-line argument. This gives the same result as if the user had typed the following command on an MS-DOS command line: ExpenseReporter filename In Visual Basic, the Command function returns the applications command-line arguments. The application can see if the arguments contain the name of a file and open the file if they do. ExpenseReporter uses the following code to do this in the Load event handler of its main form: If Command <> Then status = FileInput(Command) NewThe New menu command blanks the existing document and begins a new one. Because the current document will be erased, ExpenseReporter uses the DataSafe function to decide if the data is safe before continuing. Once it has determined that the current document is safe, the application resets controls and data structures so they represent a new document. These steps will vary from application to application. ExpenseReporter blanks most of its controls. It sets the values for the Prepaid and Advance fields to 0.00 because those values are the most common. It also calls the ComputeTotals subroutine so it displays the correct new totals, initially also 0.00 because there are no expenses on the form at this point. An application should then use the SetFocus method for one of its controls to move the cursor to the control that the user will most likely want to modify first. For the ExpenseReporter application, the user will probably begin a new expense report by filling in the employee name. The program uses the following code to set the input focus to the Name field: NameText.SetFocus The program also blanks the variable FileName to indicate that the new document does not yet have an associated file name. Finally, it sets DataModified to false. If the user wanted to open an existing file or exit the application at this point, DataSafe would return true and the application would proceed without asking if the data should be saved. This is reasonable: The user does not need to save a blank document because creating a new one at any time is easy. The following code shows the event handler that is triggered when the user selects the New command from ExpenseReporters File menu.
Private Sub mnuFileNew_Click()
See if the data is safe.
If Not DataSafe(Do you want to save the changes?) _
Then Exit Sub
Start waiting.
WaitStart
Clear all the controls.
ClearControls
Prepaid and Advance are usually 0.00.
PrepaidText.Text = 0.00
AdvanceText.Text = 0.00
Move the focus to the employee name field.
NameText.SetFocus
Blank the file name.
SetFileName
Display the zero totals.
ComputeTotals
Mark the data as not modified.
DataModified = False
End waiting.
WaitEnd
End Sub
Designing MenusDeciding what kinds of menu commands go where is fairly simple because most choices are restricted by custom. For instance, almost every Windows program in existence has a File menu at the left end of the menu bar. Some programs have no commands other than Exit in this menu. Custom dictates that the Exit command be placed at the bottom of the File menu, even though Exit really does not have much to do with files. The following sections describe standard menu design principles. These rules are not absolutely perfect, and some applications do not follow the rules closely. If an applications menus are as standard as possible, however, users will feel more comfortable with the application and will not waste time hunting for common commands. Any experienced user will know to look for printing functions and the Exit command in the File menu. Mnemonics and AcceleratorsMain menus and many common menu commands have standard mnemonic and accelerator (shortcut) keys. For example, the mnemonic for the File menu is its first letter, F. If the user presses and releases the Alt key and then the F key, the File menu should appear. In Visual Basic a menus mnemonic is determined by the letter proceeded by an ampersand (&) in the menu items caption. For example, in Visual Basics Menu Editor the caption for the File menu is displayed as &File. This makes the menus mnemonic the letter F. Visual Basic will display the mnemonic letter underlined. This special use of the ampersand character can be a problem if the program wants an ampersand to appear. For instance, menu items, labels, and command buttons display the text John Wiley & Sons, Inc. as John Wiley _Sons, Inc. To force one of these controls to display an ampersand, precede it with another ampersand, as in John Wiley && Sons, Inc. The commands within a menu should also have mnemonic letters. Usually this is the first letter in the commands name. If that letter is used by another command in the same menu, the first letter in another word in the command makes a good second choice. For instance, in most applications the Save command uses the letter S as a mnemonic, so the Save As command uses the letter A in the word As. If no first letters are available, the command should use the next letter in any word that has not yet been used by another command. If every letter in the commands name has been used, the mnemonics of the other commands should probably be changed to make a letter available. Many commands have standard accelerators. For instance, Ctrl-P is the standard accelerator for printing. If the user presses Ctrl-P, the application should print the current document. In Visual Basic an application designer assigns an accelerator to a menu item by selecting from the Shortcut combo box list in the Menu Editor.
|
||||||||||||||||||||||
|
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.
|