ADDING MENUS, MENU ITEMS, AND SUBMENUS


The first step in adding a menu system to a Visual Basic application is to add the MenuStrip control located in the Toolbox. Doing so adds an instance of the control to the component tray. Once you have done this, you can begin defining menus, as demonstrated in Figure 4.8.

image from book
Figure 4.8: Using the MenuStrip control to add a menu system to a Visual Basic application.

The following procedure outlines the steps involved in adding a menu system to your Visual Basic applications.

  1. Open your Visual Basic application and select the form to which you wish to add a menu system.

  2. Add the MenuStrip control to your form.

  3. Click on the Type Here text that is displayed, and type the name of the first menu that you wish to define, as demonstrated in Figure 4.9.

  4. To create an additional menu, click on the Type Here text shown just to the right of the first menu heading and type the name of the menu, as demonstrated in Figure 4.10.

  5. To add a menu item under a particular menu, click on the menu, click on the Type Here text that appears just underneath it, and type in the name of the menu item, as demonstrated in Figure 4.11.

  6. To create a submenu, select the menu item that will provide access to the submenu. Then click on the Type Here text located just to the right of the menu item and type in the name of the first menu item in the submenu. Then, to complete the submenu, continue clicking on the Type Here text displayed under each new menu item in the submenu, adding as many menu items as required, as demonstrated in Figure 4.12.

  7. Repeat steps 4 through 6 as many times as necessary to assemble your application's menu system.

image from book
Figure 4.9: Defining the first menu heading In the application's menu system.

image from book
Figure 4.10: Adding an additional menu to the application's menu system.

image from book
Figure 4.11: Adding a menu item under a menu heading.

image from book
Figure 4.12: Adding a submenu item to the menu system.

One important point to think about when adding menus and menu items is consistency in how you spell things. Always make the first letter in each menu or menu item a capital letter. Also, add three ellipses () to the end of any menu name that, when selected, will provide access to another window.

Trick 

You can quickly rearrange the contents of your menu systems by using drag-and-drop to move a menu item to a new Location. You can also select a menu item, right-click on it, and select Delete to remove it.

Trick 

If you want. Visual Basic can help you start putting together a generic menu system TRICK that includes menus and menu items standard to most Windows applications. To take advantage of this feature, add the MenuStrip control to a form, right-click on the control, and select Insert standard items. In response, you'll end up with a preconfigured menu system like the one shown in Figure 4.13.

image from book
Figure 4.13: Inserting a preconfigured menu system to a forms.

Associating Code Statements with Menus and Menu Items

As you may have guessed, all that you have to do to associate Visual Basic programming statements with a given menu or menu item is to double-click on it. When you do this, the IDE opens up the code editor, creates a new Click event procedure for the selected menu or menu item, and positions the cursor so that you can begin entering your code statements.

For example, Figure 4.14 shows an example of the code associated with the Exit menu.

image from book
Figure 4.14: Adding code to the Exit menu item that will close the application when the user clicks on it.

Enhancing Windows Menus

As shown in the following list, you can do a number of things to make your menu systems easier and more convenient for users:

  • Adding shortcut keys

  • Adding access keys

  • Adding and removing check marks

  • Organizing menu items using separator bars

Adding Shortcut Keys

Shortcut keys provide users with the ability to access menu items using only the keyboard. Shortcuts can be used to provide fast access to commonly used application commands. Examples of shortcut keys include all of the function keys (F1 to F12) and keystroke combinations such as Ctrl + N and Ctrl + O.

Trap 

A number of shortcut keys are so commonly used that they have become a de facto standard for Windows applications. Examples include Ctrl + S for saving and Ctrl + X for exiting an application. Make sure that you use these shortcuts when appropriate in your applications.

Adding a shortcut to a menu item is very straightforward, as outlined by the following procedure.

  1. Select the menu containing the menu item that you wish to work with.

  2. Select the appropriate menu item.

  3. Click on the value field associated with the Shortcut property (in the Properties window) and select one of the shortcuts displayed in the drop-down list, as demonstrated in Figure 4.15.

  4. Once selected, the shortcut will be immediately visible, as demonstrated in Figure 4.16.

image from book
Figure 4.15: Specifying the shortcut you wish to assign to a menu or menu item.

image from book
Figure 4.16: Adding shortcut keys to menus and menu items.

Adding Access Keys

Access keys also allow users to activate specific menu items using their keyboard. To set up an access key, you must designate one of the characters in a menu's or menu item's name as an access key. Access keys are identified by the presence of an underscore character under the letter in the menu or menu items name that represents the access key. For example, the letter F is generally used as the access key for the File menu.

To use an access key, all the user has to do is press and hold the Alt key while simultaneously pressing the appropriate letter key. For example, to active the File menu in Visual Basic Express 2005, all you have to do is press and hold Alt and F at the same time. While the File menu is displayed, you can release the Alt key and press the access key for any menu item located under the File menu to access it.

The following procedure outlines the steps involved in adding access keys to your Visual Basic menu and menu items.

  1. Select the menu heading or menu item that you wish to work with.

  2. Position the cursor in front of the letter in the menu or menu item name that you want to designate as the access key.

  3. Add the ampersand (&) character just in front of the selected letter (for example &File or &Exit). The results will be immediately visible, as demonstrated in Figure 4.17.

image from book
Figure 4.17: Adding access keys to menus and menu items.

Adding and Removing Check Marks

Another feature that you may want to implement when creating a menu system for your applications is the use of check marks. Check marks are used to identify the status of menu items that can be toggled on and off. The presence of a check mark indicates that the menu item has been selected or enabled. Similarly, the absence of a check mark indicates that the menu item has been deselected or disabled. For example, later in this chapter when you develop the Lottery Assistant game, you will use check marks to show the player which font and background colors have been selected.

You can set an initial check mark setting at design time using the following procedure:

  1. Select the menu item that you wish to work with.

  2. Right-click on the menu item and select Checked from the popup menu that appears, as demonstrated in Figure 4.18.

image from book
Figure 4.18: Using a check mark to indicate when a menu item has been selected.

Trick 

You can also set the check mark value by selecting the appropriate menu item and then setting the CheckState property in the Properties window to Checked.

Any menu item that can be checked can also be unchecked. When clicked, it is appropriate to switch the checked status of a menu item. Therefore, you will need to know how to pro-grammatically change the checked status of your menu items at run time, as demonstrated in the following example.

 Private Sub GrayToolStripMenuItem_Click(ByVal sender As System.Object, _        ByVal e As System.EventArgs) Handles GrayToolStripMenuItem.Click          GrayToolStripMenuItem.Checked = True          YellowToolStripMenuItem.Checked = False          WhiteToolStripMenuItem.Checked = False End Sub 

Here the statement marks the Gray menu item as checked whenever the user clicks on it and removes check marks from the Yellow and White menu items. Alternatively, if the user comes back later and clicks on the Yellow menu item, it's appropriate to remove the check mark from the Gray and White menu items and add it to the White menu item, as demonstrated below.

 Private Sub YellowToolStripMenuItem_Click(ByVal sender As System.Object, _        ByVal e As System.EventArgs) Handles YellowToolStripMenuItem.Click          GrayToolStripMenuItem.Checked = False          YellowToolStripmenuItem.Checked = True          WhiteToolStripMenuItem.Checked = False End Sub 

Organizing Menu Items Using Separator Bars

To make a lengthy list of menu items easier for users to work with, you can visually group related menu items together. For example, in Microsoft Excel, menu items for opening and closing files, saving files, printing files, and closing the application are all grouped separately. Of course, in order to use the separator bar to organize menu items, you must have grouped them together in the first place. The following procedure outlines the steps involved in adding a separator bar between your menu items.

  1. Begin creating your menu items.

  2. When you get to a point where you wish to insert a separator bar, add a temporary menu item at this location.

  3. Right-click on the temporary menu item and select the Convert To option on the pop-up menu that appears. This displays a new list of available conversation items. Select the Separator option, as demonstrated in Figure 4.19.

image from book
Figure 4.19: Using separator bars to visually group related menu items together.

Enabling and Disabling Menus and Menu Items

Based on the current status of your application, there may be times when you want to prevent a user from being able to click on a given menu item. For example, later in this chapter you will work on developing the Lottery Assistant game. This game will be controlled by its menu system, which will include Get Numbers and Clear Numbers menu items on its File menu. The game will enable and disable access to the Get Numbers menu item based on whether or not the user has entered all the data required to retrieve lottery numbers.

When a menu item is disabled, it will appear to the user to be grayed out and won't respond when clicked. By default, menu items are enabled. However, you can disable menu items at design time using the following procedure.

  1. Select the menu or menu item that you wish to work with.

  2. Using the Properties window, set the Enabled property for the menu or menu item to True to enable the menu or menu item (the default) or set it to False to disable it. The results will be immediately visible, as demonstrated in Figure 4.20.

image from book
Figure 4.20: Disabling a menu item at design time.

Of course, for things to work, you need to add program statements to your code that enable your menu items at the appropriate time. Doing so is straightforward, as demonstrated below.

 Private Sub Button1_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles Button1.Click         CustomToolStripMenuItem1.Enabled = True End Sub 

Trap 

If you disable a menu heading or a menu item that provides access to a submenu, then all menu items and submenus underneath it will be hidden from view, as demonstrated in Figure 4.21.

image from book
Figure 4.21: Disabling a menu item and its associated submenu.

Hiding and Displaying Menus and Menu Items

Depending on what your application is doing, there may be times where you feel that simply enabling and disabling menus and menu items is not enough. In these circumstances, you can go a step further by hiding and later redisplaying menus and menu items. The following procedure outlines the steps involved in hiding and displaying menus and menu items.

  1. Select the menu or menu item that you wish to work with.

  2. Using the Properties window, set the Visible property for the menu or menu item to True to enable its display (the default) or set it to False to disable it. The results will be immediately visible, as demonstrated in Figure 4.22.

image from book
Figure 4.22: Making the Custom submenu item invisible on the Background submenu.

Trap 

If you hide a menu or a menu item that provides access to a submenu, then all menu items and submenus underneath it will be hidden from view.

Once hidden, you'll need to add program statements to your code that make menu or menu items visible later on. Doing so is straightforward, as demonstrated below.

 Private Sub Button1_Click_1(ByVal sender As System.Object, _    ByVal e As System.EventArgs) Handles Button1.Click         CustomToolStripMenuIteml.Visible = True End Sub 

As Figure 4.23 demonstrates, the Custom menu item is once again visible once this above procedure as been executed.

image from book
Figure 4.23: You can also control user access to menu items by hiding and redisplaying them when appropriate.




Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner
Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner
ISBN: 1592008143
EAN: 2147483647
Year: 2006
Pages: 126

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net