Sie sind auf Seite 1von 26

Creating Menus in Your Programs

Windows Standard Menu Conventions


Feature Caption Organization Convention Use one or two short specific words. Menu items should be grouped logically by function and allow for a minimal number of levels to access each feature. Each menu item should be assigned an access key (the underlined letter in a menu or menu selection) to allow for keyboard access to the menu choices. The key should be unique in each section of the menu and is normally the first letter of the caption. Any menu features that are frequently used or need to be available from any part of the program should be assigned a shortcut key. Each shortcut key can be assigned to only one menu item. Menu items that simply set or clear a single program option should contain a checked feature directly in the menu.

Access keys

Shortcut keys

Check box

Using Visual Basic's Application Wizard


Visual Basic 6 has improved the Application Wizard to allow for the creation of fully customized menus directly in the wizard. In the earlier versions of the wizard, you could select from only a limited set of standard menu options to include in your application. This improvement makes the wizard a useful tool and a good starting point for building and understanding your first menu. The primary benefit of VB's Application Wizard is that it can make menus that have the Windows standard features already loaded. You simply select the features you want from the template provided. They're also already arranged in the Windows standard layout. Limitations of the Application Wizard You can't use the Application Wizard to modify existing projects. Many thirdparty support programs add more functionality to your project without having you do more programming. You can also use the add-ins that come with Visual Basic, such as the Menu Editor.

The Application Wizard is a straightforward tool for building a new application. This means that the Application Wizard is used to build a functional application shell with standard features. If you want additional features, you have to program them yourself or use the Menu Editor to add them. After you click Finish in the Application Wizard and the base program is generated, you're on your own to make changes to the program by using the Menu Editor

Build a simple menu with the Application Wizard


1. Start the Application Wizard through the default dialog that opens when Visual Basic 6.0 starts or by choosing New Project from the File menu Double-click the Application Wizard icon to start the wizard 2. The wizard's Introduction dialog allows you to reuse the answers you saved during a previous Application Wizard session Leave the default choice and choose Next. 3. In the Interface Type dialog, select the type for the initial application screen For this sample application, select Single Document Interface. Leave the default project name and choose Next. 4. A Windows standard default menu is created for you to begin to modify. Not all possible choices have been selected in the initial menu, After you make your modifications, choose Next. 5. The Application Wizard allows for the customization of a toolbar, resource file, browser, database connectivity, and other templates. For this example, skip these dialogs; choose Next five times to get to the final dialog.

6. In the last dialog, you can save your profile Enter a name for your profile that will be easy to relate to your application. After you enter the name, choose Finish to complete the Application Wizard. (Clicking Finish on any of the earlier dialogs in the wizard bypasses the option to save the profile for future use.)

Using Visual Basic's Menu Editor


The Menu Editor allows you to create menu bars from scratch or modify already created menus. As with the Application Wizard, these menus are located at the top of a form, with associated dropdown submenus or pop-up menus that users typically access by right-clicking. Make a simple menu with the Menu Editor
1. Open a new project. Save the project with a new filename. Rename the default form (with a name such as frmMenu) and save it to a new filename, such as frmMenu.frm.

Don't press Enter yet! Press Tab or use the mouse to move between text boxes in the Menu Editor; otherwise, you cause the Menu Editor to create a new menu item.

Using ampersands Including an ampersand in the menu caption forces the character that comes after it to be underlined when it appears in the menu. The underlined character becomes the hot key, which users can press (combined with the Alt key) to make a selection. For example, with the F underlined on the File menu, users can press Alt+F to pull down that menu
2. Open the Menu Editor by clicking the Menu Editor button on the Standard toolbar. If the form doesn't have the focus, the Menu Editor icon is grayed out. 3. In the Menu Editor dialog, type &File in the Caption text box and mnuFile in the Name text box. Click Next. 4. Click the button with the arrow pointing to the right. This is the indent button.

5. Type E&xit in the Caption text box and itmExit in the Name text box. Your menu should appear in the dialog as shown in Figure 13.6. 6. Click OK. 7. The menu you created is embedded in the form Drop down the File menu that you just created and click Exit. The code window for the itmExit_Click() event procedure appears. 8. Add the Unload Me statement to the itmExit event procedure 9. Press F5 to run the code

You then enter identifiers for the Caption and Name for each menu item. (The Caption is actually the screen name of the item, as it appears in the Menu Bar or within the dropdown menu. The Name is used only in Visual Basic code it is not displayed when the application is running.) The Caption will appear in the large area at the bottom of the Menu Editor as well as within the Caption field. You may either press the Enter key or click on the Next button after the information has been entered for each menu item.

The menu headings must be flush left within each line. Items that appear within each menu must be indented one level, as indicated by four ellipses preceding each item. The indentation is accomplished using the right-arrow button. Click once to indent one level (four ellipses). The opposite action, i.e., moving an indented item to the left, is accomplished with the leftarrow button. The relative ordering of each menu component can be altered using the up- and down-arrow buttons. Thus, to move an entry ahead of the two preceding entries, highlight the entry to be moved and click on the up-arrow button twice. In addition, a menu component can be inserted by highlighting the item that will appear below the insertion, and clicking on the Insert button. Similarly, a menu component can be deleted by highlighting the component and clicking on the Delete button. Remember that it is the indentation pattern that distinguishes the menu heading from the corresponding items in the drop-down menu. Also, it is the order of the entries that distinguishes one set of menu components from another. (This may sound more complicated than it really is, as shown in the following example.)

Property Caption Checked Enabled Name Property Shortcut

Value/Type Description String Boolean Boolean String The text that appears on the menu bar. Puts a check mark before the Caption string of a menu item. Doesn't gray out the Caption string if True.

The name of the object--available only at design time. Value/Type Description N/A A key combination that allows you to access the menu item's functionality. You can choose this setting only at design time from a list that appears in the Shortcut drop-down list in the Menu Editor. Makes a top-level menu in an MDI form display a list of windows open in that MDI form-available only at design time.

WindowList

Boolean

Adding Access Keys to Menu Items


In addition to clicking a menu item to perform a task, you can access a menu item's functionality by using access keys. Access keys allow users to perform menu selections by pressing Alt and then the assigned letter of the access key. After a menu is open, users select from the menu by pressing the access key for the desired menu choice. An access key is denoted by an underlined character in a menu item's Caption--for example, the F in File on a standard Windows menu bar. When you press Alt+F, the result is the same as if you had clicked File on the menu bar. To denote an access character, put an ampersand (&) before the character you want to use. the ampersand indicates that the letter F in File will be underlined. Shortcut keys are assigned in the Menu Editor before compiling the program and can't be changed in the runtime environment.

Adding Shortcut Keys to Menu Items


Using shortcut keys (also known as accelerator keys) is another way of performing menu functions from the keyboard. You set a shortcut key combination by choosing a combination from the Menu Editor's Shortcut drop-down list Shortcut keys work anywhere in the program, whether or not the menu item is visible. That's why they're called shortcuts--they're active all the time, and users can jump to the function without going through the menu selection process.

Adding Separator Lines to Menus


You add separator lines to a menu by typing a single hyphen (-) in the Menu Editor's Caption text box. &Continents ....&Africa ....As&ia ....&Europe ....&North America ....&South America ....A&ustralia ....An&tarctica

SUBMENUS
A menu item may have a submenu associated with it. Placing the mouse over the menu item

Creating Pop-Up Menus


There are two types of menus: menu bars and pop-up menus. A menu bar is the type that you made with the wizard and edited in the Menu Editor--a sequence of menus embedded on a form. A pop-up menu is, as the name implies, a menu that pops up from somewhere on a form. When you rightclick in the Windows Explorer, for example, a pop-up menu appears. You can make any menu appear as a pop-up menu by using a form's PopupMenu method: PopupMenu mnuFile A menu command can exist within a menu bar and as a pop-up menu, if you wanYou need to create a form to go with the pop-up menu because the PopupMenu method must be part of a form. The following code demonstrates how this feature works: Private Sub Form_MouseDown (Button As Integer, _ Shift As Integer, X As Single, Y As Single If Button = 2 Then PopupMenu mnuFile End If End Sub For this code to work, you need to create a form that includes a Menu control to be named mnuFile, which must have at least one submenu. You need to program this code into the form's Declarations section and press F5 to run it.

MORE ABOUT THE MsgBox FUNCTION


integer variable = MsgBox(string, integer, title) The value of the integer argument (default 0) defines the command buttons that appear within the dialog box. Also, title represents a string that will appear in the message boxs title bar. Its default value (if not included as an explicit argument) will be the project name.

The value returned by the MsgBox function will depend upon the particular command button selected by the user during program execution. The possible values are summarized below.

THE InputBox FUNCTION


The InputBox function is similar to the MsgBox function. However, this function is primarily intended to display a dialog box that accepts an input string, whereas the MsgBox function is primarily intended to show an output string.

string variable = Input Box(prompt, title, default) The first argument (prompt) represents a string that appears within the dialog box as a prompt for input. The second argument (title) represents a string that will appear in the title bar. Its default value (if not included as an explicit argument) will be the project name. The last argument (default) represents a string appearing appearing initially in the input boxs text box. The default string will be empty if this last argument is not included in the function reference.

Private Sub Command1_Click() Dim CustomerName As String, L1 As String, L2 As String Dim AcctNo As Integer, Verify As Integer L1 = "Customer Name: " L2 = "Account Number: " Do Label1 = L1 Label2 = L2 CustomerName = InputBox("Please enter your name:", "Customer Name") Label1 = L1 & CustomerName AcctNo = Val(InputBox("Please enter your account number:", "Account Number")) Label2 = L2 & Str(AcctNo) 'Process the account

Verify = MsgBox("Is this correct?", 3, "Verify") Loop While Verify = 7 If Verify = 2 Then Label1 = L1 Label2 = L2 End If End Sub Private Sub Command2_Click() End End Sub

Das könnte Ihnen auch gefallen