Visual C++ .NET Controls: MainMenu


The Main Menu



A menu of a computer program is a list of actions that can be performed on that application. To be aware of these actions, the list must be presented to the user upon request.

A menu is considered a main menu, when it carries most of the actions the user can perform on a particular application. Such a menu is positioned on the top section of the form in which it is used. By design, although a main menu is positioned on a form, it actually belongs to the application.

A main menu is divided in categories of items and each category is represented by a word. On the Visual Studio's IDE, the categories of menus are File, Edit, Project, Window, etc. To use a menu, the user first clicks one of the words that displays on top. Upon clicking, the menu expands and displays a list of items that belong to that category.

There is no strict rule on how a menu is organized. There are only suggestions. For example, actions that are related to file processing, such as creating a new file, opening an existing file, saving a file, printing the open file, or closing the file usually stay under a category called File. In the same way, actions related to viewing documents can be listed under a View menu.

Main Menu Creation


The main menu is implemented through the MainMenu class. To create a main menu, on the Toolbox, you can click the MainMenu button and click the form that will use the menu. After clicking the form, an empty menu is initiated, waiting for you to add the necessary menu item.

To create a menu category that starts a list of items that belong to the menu object, click the Type Here line and type the desired item then press Enter.

If you look at the main menu of Visual Studio, you would see that a letter on each menu is underlined. This letter allows the user to access the menu using a keyboard. For example, if the letter e is underline in a File menu as in File, the user can access the File by pressing the Alt then the E keys. To create this functionality, choose a letter on the menu item and precede it with the & character. For example, &File would produce File.

A shortcut is a key or a combination of keys that the user can press to perform an action that would also be performed using a menu item. When creating a menu, to specify a shortcut, use the Shortcut field in the Properties window.

If you have used applications like Microsoft Word or Adobe Photoshop, you may know that they don't show all of their shortcuts on the menu. If you want to hide a shortcut, after specifying it, in the Properties window, set the ShowShortcut property to False.

  1. Start a new Windows Forms Application (.NET) named Menus1
  2. Change the form's Text to Employees Related Issues
  3. On the Toolbox, click the MainMenu button and click on the form
  4. On the form, click Type Here, type &Staff and press Enter
  5. On the form, click Staff and click the Type Here box under it
  6. Type &New Hire
  7. In the Properties window, click Shortcut. Click the arrow of the Shortcut right field and select CtrlN
  8. On the form, click the Type Here line under New Hire and type &Records
  9. Using the Properties window, set the Shortcut to CtrlR
  10. On the form, click the item under Records and type Time &Sheet...
  11. Set its Shortcut to CtrlM
  12. Set its ShowShortcut property to False
  13. On the form, click Records and press Insert
  14. Type Searc&h...
  15. To add a separator, click the Type Here line under Time Sheet
  16. Type - and press Enter
  17. Click the empty item under the previously added separator line, type E&xit and press Enter
  18. To move the Search item, on the form , click and drag Search down:
    Moving a menu item
  19. When it is positioned under Time Sheet item, release the mouse.
  20. To start a new menu category, click the Type Here box on the right side of Staff
  21. Type &Books and press Enter
  22. Click Books and click the empty item under it
  23. Type &New and press Enter
  24. Type Show All &Titles and press Enter
  25. To create a submenu, click New and click the Type Here box on its right
  26. Type &Title and press Enter
  27. Type &Author and press Enter
  28. Type &Category and press Enter
  29. Click the Type Here box on the right side of Books. Type &View and press Enter
  30. Click View and click the empty box under it
  31. Type &Toolbar and press Enter
  32. Type &Status Bar and press Enter
  33. Click Toolbar to select it
  34. To put a check mark on a menu item, on the Properties window, set the Checked property to True
  35. Also, for the Status Bar item, set the Checked property to True
  36. To move the whole View menu category, click and drag View to the left:
    Moving a menu category
  37. When it is positioned between Staff and Book, release the mouse


Coding a Main Menu Item

If you create a menu as we have just done, to write code for one of the menu items, you can double-click the menu item. This would open the Click event of the menu item in the Code Editor and you can start writing the desired code for that item.

  1. On the form, click Staff and double-click Exit
  2. Implement the event as follows:
    System::Void menuItem7_Click(System::Object *  sender, System::EventArgs *  e)
  3. Test the form
    Using a menu item at execution time
  4. To close it, click Staff -> Exit

Home Copyright © 2002-2004 FunctionX, Inc.