ADDING MENUS, MENU ITEMS, AND SUBMENUS


The first step in adding a menu system to a Visual C++ application is to select the MenuStrip control from the Toolbox and add it to your form. Doing so creates an instance of the control to the Component Tray. After this control is a part of your form, you can begin defining menus, as demonstrated in Figure 4.8.

image from book
Figure 4.8: You can use the MenuStrip control to add a menu system to a Visual C++ application.

The following steps illustrate the process of adding a menu system to your Visual C++ applications.

  1. Open your Visual C++ application and select the form to which you want 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 want 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 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. (See 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: The first menu heading in the application's menu system is defined by typing its name.

image from book
Figure 4.10: Additional menus can be added to the application's menu system.

image from book
Figure 4.11: Menu items can be added by typing in the boxes under a menu heading.

image from book
Figure 4.12: Entering text automatically enables you to add submenu items to the menu system.

One way to make your menus look more professional is to be consistent in how you spell each element. Always be sure that the first letter in each menu or menu item is a capital letter. Also, add an ellipsis () to the end of any menu name that, when selected, provides access to another window.

Trick 

After you've started defining your menu items, you're not stuck with their position. You can select a menu item and quickly rearrange it by using drag-and-drop to move it to a new location. You can also right-click on it and select Delete to remove it.

Trick 

If you want, Visual C++ can help you start putting together a generic menu system that includes menus and menu items standard to most Windows applications. To use this feature, add the MenuStrip control to a form, right-click on the control, and select Insert Standard Items. In response, you end up with a standard menu system like the one shown in Figure 4.13.

image from book
Figure 4.13: Visual C++ allows you to insert a standard menu system into a form.

Associating Code Statements with Menus and Menu Items

Just as you have done with other controls, you can give menu items code by double-clicking on them. When you do so, the IDE creates a new click event function for the item and positions the cursor so that you can begin entering your code statements.

For example, Figure 4.14 illustrates the standard menu added in the previous example. You can add code by double-clicking on any item. You can, for example, associate the following code with the Exit menu to close the application.

image from book
Figure 4.14: Clicking any menu item allows you to easily add code to it.

 private: System::Void exitToolStripMenuItem_Click(System::Object^           sender, System::EventArgs^  e)           {             Close();           } 

Enhancing Windows Menus

As shown in the following list, you can do a variety 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 allow users to access menu items using only the keyboard. This can give your users a boost in productivity. You can use shortcuts to quickly initiate commonly used application commands. Examples of shortcut keys include all the function keys (F1 to F12) and keystroke combinations such as Ctrl+N and Ctrl+O.

Hint 

Some shortcut keys have become so common that they are now a de facto standard for Windows application commands. 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 straightforward, as outlined here:

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

  2. Select the appropriate menu item.

  3. Click on the value field that is 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.

image from book
Figure 4.15: You can specify the shortcut you want to assign to a menu or menu item.

After you select the shortcut, it's immediately visible, as demonstrated in Figure 4.16.

image from book
Figure 4.16: You can specify shortcut keys to activate menus and menu items.

Adding Access Keys

Access keys are another method for allowing users to activate specific menu items using their keyboard. To establish an access key, preface one of the characters in a menu's or menu item's name with a special character. You can identify access keys by their underscore character under the letter in the menu or menu item's name. For example, the letter F is generally used as the access key for the File menu.

To use an access key, all you have to do is press and hold the Alt key while simultaneously pressing the appropriate letter key. For example, to open the File menu in Visual C++ Express 2005, just 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 steps outline the process involved in adding access keys to your Visual C++ menu and menu items.

  1. Select the menu heading or menu item that you want 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 in front of the selected letter (for example &File or E&xit). The results are immediately visible, as demonstrated in Figure 4.17.

image from book
Figure 4.17: This example shows the process of adding access keys to menus and menu items.

Adding and Removing Check Marks

Another feature that users might find convenient is the check mark. Check marks are useful when you need to identify the status of menu items that you can toggle 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 use check marks so that the user can distinguish which font and background options are selected.

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

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

  2. Right-click to bring up the item's context menu. Click on the Checked option, as demonstrated in Figure 4.18.

image from book
Figure 4.18: A check mark indicates 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 Checked property in the Properties window to true.

Any menu item that can be checked can also be unchecked. When you click a menu item, it is appropriate to toggle the checked status of the item. Therefore, you need to know how to change the checked status of your menu items at runtime, as demonstrated in the following example:

 private: System::Void grayToolStripMenuItem_Click(System::Object^           sender, System::EventArgs^  e)           {             grayToolStripMenuItem->Checked = true;             yellowToolStripMenuItem->Checked = false;             whiteToolStripMenuItem->Checked = false;           } 

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 Yellow menu item, as demonstrated next:

 private: System::Void yellowToolStripMenuItem_Click(System::Object^           sender, System::EventArgs^  e)           {             grayToolStripMenuItem->Checked = false;             yellowToolStripMenuItem->Checked = true;             whiteToolStripMenuItem->Checked = false;           } 

Organizing Menu Items Using Separator Bars

Separator bars divide long lists of menu items and make them easier to read. They are also an excellent way of visually grouping related menu items. For example, in Microsoft Excel, menu items for opening and closing files, saving files, printing files, and closing the application are grouped separately.

Of course, the separator can group menu items only if you have added related items together in an easy-to-understand fashion. The following steps outline the process 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 want to insert a separator bar, click the drop-down arrow next to Type Here.

  3. A list of options appears. Select the Separator option from the list. A bar is added to your menu, separating its contents, as shown in Figure 4.19.

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

Enabling and Disabling Menus and Menu Items

Based on what your application is doing, you might need to prevent a user from being able to click on a given menu item. For example, later in this chapter, you work on developing the Lottery Assistant game. This game's menu system controls it, and it includes Get Numbers and Clear Numbers menu items on its File menu. The game enables and disables access to the Get Numbers menu item based on whether the user has entered all the data required to retrieve lottery numbers.

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

  1. Select the menu or menu item that you want 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. As you can see in Figure 4.20, the Yellow menu item has been disabled.

image from book
Figure 4.20: Menu items can be disabled at design time.

This functionality is not very useful, however, if you don't add program statements to your code that enable your menu items at the appropriate time. Doing so is a simple process, fortunately, as demonstrated next. In the following example, the previously disabled Yellow menu option is enabled when the user selects the White menu option.

 private: System::Void whiteToolStripMenuItem_Click(System::Object^           sender, System::EventArgs^  e)           {             yellowToolStripMenuItem->Enabled = true;           } 

Trap 

If you disable a menu heading or a menu item that provides access to a submenu, all menu items and submenus underneath it become inaccessible. Figure 4.21 demonstrates this. Notice the arrow to the right of the Yellow menu option. This menu option has three submenu items, none of which is accessible as long as the Yellow menu option is disabled.

image from book
Figure 4.21: Associated submenus are disabled when a menu item is disabled.

Hiding and Displaying Menus and Menu Items

Although disabling menu items is a frequent technique that applications use to hide options that are not relevant or appropriate to whatever your user is currently doing, sometimes this is not enough. In such cases, you can go a step further by completely hiding and later revealing menus and menu items. The following steps outline the process involved in hiding and displaying menus and menu items:

  1. Select the menu or menu item that you want 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 option disappears at runtime. Figure 4.22 shows the result of adding an option named Custom to the Background submenu at design time, which simply disappears when its Visible property is set to false.

image from book
Figure 4.22: Here the Custom submenu item has been made invisible on the Background submenu.

Trap 

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

After you hide a menu or menu item, you need to add program statements to your code that make them visible later on. Doing so is straightforward, as shown in the following example:

 private: System::Void loadToolStripMenuItem_Click(System::Object^           sender, System::EventArgs^  e)           {             customToolStripMenuItem->Visible = true;           } 

As Figure 4.23 demonstrates, the Custom menu item is once again visible. In this example, the code that triggers this change was placed in the Load option, so that when the Load menu item is selected, the Visible setting for the Custom item is set to the value of true.

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




Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner 2006
Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner 2006
ISBN: 735615381
EAN: N/A
Year: 2005
Pages: 131

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