Using Menus


Menus are a well-known feature of most GUI programs, and the Forms namespace contains a complete set of classes for building and working with menus.

Menus in Windows Forms applications are represented by two main classes: MainMenu represents the menu bar that sits at the top of a form, while MenuItem represents all the items that make up the menus attached to a MainMenu. A MainMenu has a collection of MenuItems, and to model the hierarchical nature of menus, MenuItems themselves can have collections of other MenuItems.

In use, menu items bear some similarity to buttons: they are both distinguished by their text, and both fire a Click event when they are selected. It probably won’t surprise you to learn that you set up handlers for menu items in exactly the same way as you do for buttons.

start sidebar
Menu Design

There are a number of well-established guidelines relating to how menus ought to be constructed and presented to the user, and I’ll give a brief outline of some of the more important ones here.

First, it is convention that the leftmost menu on the menu bar is called File and the last item on the File menu is the one that exits from the program. It’s usually called Exit. Likewise, the rightmost item on the menu bar should be the Help menu, which will contain entries to help the user access the Help system and probably an entry to show the program’s About box. Menu items that are going to display a dialog box should end with an ellipsis (...); for example, About.... Menu items that are going to have an immediate action, such as exiting the application, shouldn’t use an ellipsis.

To help users navigate around menus, assign access keys to menus by putting an ampersand before one of the letters in the menu caption; for example, &File associates the access key F with this menu item. Users can navigate through menus by holding down Alt and typing the access keys. You can also assign shortcuts to individual menu items, such as Ctrl+P for print, and these should be added to the most frequently used menu items.

Drop-down menus shouldn’t be too long; any more than 10 entries is probably excessive. Consider using hierarchical menus.

Menus shouldn’t let the user do anything that isn’t sensible. For example, if there are no files open, an application shouldn’t let the user select the Print option. There’s nothing to print, so it’s just confusing to be able to select the item. It’s good practice to dim (disable) menu items that don’t apply in the current context. Some applications dynamically add and remove menu items, but this tends to lead to confusion because users can’t remember how to make a particular menu item reappear!

end sidebar

The following exercise will show you how to add a simple menu to the application’s main form.

  1. Continue working with the same project you’ve been using throughout this chapter.

  2. Drag a MainMenu from the Toolbox, and drop it onto the form. You will see that an icon is displayed in a special area at the bottom of the designer screen.

    click to expand

    Controls that have no visible presence on the form display in a separate area at the bottom of the designer. Only controls that can be seen at run time display on the form.

    You will see a menu-like feature has been added to the top of the form, with a rectangle toward the left that contains the text Type Here. Click in that rectangle, and type &File to add a menu item.

  3. Once you’ve typed the entry, you will see that two more Type Here boxes appear. The one to the right is to add more items to the menu bar, while the one below is to add more items to the File menu. Add &About... and E&xit items to the File menu.

  4. Select the About menu item, and bring up the Properties editor. Change the name of the About menu item to aboutMenuItem. Change the name of the Exit menu item to exitMenuItem.

  5. You provide menu items so that the user can select them and execute code. Double-click the About menu item to add a handler, and add the following code to the function:

    private: System::Void aboutMenuItem_Click(System::Object * sender, System::EventArgs * e) { MessageBox::Show(S"The About menu item", S"Menu"); }

    Add a second handler for the Exit menu item, but this time use it to exit from the application.

    private: System::Void exitMenuItem_Click(System::Object * sender, System::EventArgs * e) { // Exit from the application Application::Exit(); }

  6. Build and run the program. You should see that the form now has a menu bar at the top, and you should be able to select the menu items.

    click to expand

More About Menus

Now that you’ve mastered the basics of adding menu support to programs, I’ll briefly mention some of the other features of the menu classes.

First, you can create hierarchical menus. Each menu item has a Type Here box to its right, and you can use this box to start a new hierarchical menu.

To add a separator bar to a menu, select the menu item above which you want the separator. Right-click the item, and select Insert Separator from the context menu.

The Checked and Enabled properties can be used to display a check mark next to a menu item and to dim menu items that aren’t currently active. Simply set the requisite property to true or false in the Properties editor, as required. Note that the Properties editor gives you a way to set the initial values of properties. You can always change them later programmatically.

Displaying a Context Menu

Most GUI applications nowadays use context menus—small menus that pop up when you right-click over a window and that are used to provide menu items specific to that part of the GUI. You can easily add a context menu to a form by creating a ContextMenu object, adding menu items to it, and then assigning it to the form’s ContextMenu property.

The following exercise shows you how to add a context menu to a form.

  1. Continue working with the same project you’ve been using throughout this chapter.

  2. Drag a ContextMenu from the Toolbox onto the form. It will display at the bottom of the form, next to the MainMenu.

  3. Add some items to the ContextMenu in the same way that you did for the main menu. You will see that the menu items display at the top of the form, as if you were editing a main menu.

  4. Assign the context menu to the ContextMenu property of the form, using the Properties editor.

  5. Build and run the code. You’ll find that you can display the context menu by right-clicking the form.

You use handlers with items on context menus in exactly the same way as with main menus.




Microsoft Visual C++  .NET(c) Step by Step
Microsoft Visual C++ .NET(c) Step by Step
ISBN: 735615675
EAN: N/A
Year: 2003
Pages: 208

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