Text OnlySkip NavigationMyHomeAccount InfoSettingsHelp SupportLogout
Books24x7 Personal account | University of Regina
Content
Browse Tools: 
Bookmark this location Bookmark this location and attach a Note Add to Bookshelf folders Purchase a printed copy of this book
View Browse Tools with Text   Plus text
Tips Tips
 
 
Previous Chapter Chapter 16 Next Chapter
Overview
Windows Forms Applications
The System::Windows::Forms Namespace
Creating and Using Forms
Using Controls
Using Menus
More About Menus
Displaying a Context Menu
Chapter 16 Quick Reference
Microsoft Visual C++ .NET Step by Step - Version 2003
Chapter 16 - Introducing Windows Forms
Microsoft Visual C++ .NET Step by Step - Version 2003
by Julian Templeman and Andy Olsen 
Microsoft Press 2003
Has CD Content
Previous Section
Progress: Progress:Progress: 55% Progress:
Next Section

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.

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.

    Image from book

    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.

    Image from book
  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.

    Image from book

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.

Image from book

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.

Image from book

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.

    Image from book

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

Previous Section
Progress: Progress:Progress: 55% Progress:
Next Section
SkillSoft