![]() ![]() | ||
Menus like File or Edit and the actual items in such menus are supported with the MenuItem class. This class supports the actual controls in your menu system, and it's their Click event that you add code to in order to make that menu system active.
This class provides properties that enable you to configure the appearance and functionality of a menu item. To display a checkmark next to a menu item, use the Checked property. You can use this feature to identify a menu item that is selected in a list of mutually exclusive menu items. You can use the Shortcut property to define a keyboard combination (like Ctrl+X) that can be pressed to select the menu item, and set the ShowShortcut property to True to display that key combination in the menu item's caption. MenuItem objects themselves also can have other MenuItem objects attached to them to display submenus.
To set the caption of a menu or menu item, you use the Text property. Setting the Text property to a hyphen (-) converts the menu item into a menu separator, one of those horizontal bars that help group menu items together. (You can even have separators in menu bars, in which case they're vertical.) Prefacing a character in a menu item's caption with an ampersand (&) underlines that character and makes it into an access key, which means the user can select that item by pressing Alt and that character. For example, giving a menu item the caption "E&xit" makes X into the access key for this menu item. You can enable and disable menu items with the Enabled property, and show or hide them with the Visible property.
Note that for a MenuItem to be displayed, you have to add it to a MainMenu (or ContextMenu) object.
MenuItem objects in a Multiple Document Interface (MDI) application work in a special way. When an MDI child window appears, its menu is merged with the MDI parent window (so no menu system appears in the child). You can specify how menu items should be added to the MDI parent window with the MergeOrder and MergeType properties. You can also use the MergeMenu method to specify how this merging occurs.
Tip |
MenuItem objects cannot be used in multiple places at the same time, such as in a MainMenu object and a ContextMenu object. However, you can use the CloneMenu method to create a copy of a MenuItem object for use in another location. |
The most common menu item event that you handle is Click, which means the user has clicked a menu item and your code should respond to it. However, there are other events here as well—the Popup event lets you to perform tasks before a menu is displayed, because it happens just before a menu item is displayed. And the Select event happens when a menu item is selected (that is, highlighted). This enables you to perform tasks like displaying help for menu items when the user places the mouse cursor over those items.
![]() ![]() | ||