How to build menus?
You add a menu component to your form, or forms, for every menu you want to include in your application. You can build each menu structure entirely from scratch, or you can start from one of the predesigned menu templates.
For more information about menu templates, see Using menu templates.
For more information about creating a menu using the menu designer see
- Naming menus
- Naming the menu items
- Adding, inserting, and deleting menu items
- Creating submenus
- Adding images to menu items
- Viewing the menu
As with all components, when you add a menu component to the form, C++Builder gives it a default name; for example, MainMenu1. You can give the menu a more meaningful name.
C++Builder adds the menu name to the form’s type declaration, and the menu name then appears in the Component list.
Naming the menu items
In contrast to the menu component itself, you need to explicitly name menu items as you add them to the form. You can do this in one of two ways:
- Directly type the value for the Name property.
- Type the value for the Caption property first, and let C++Builder derive the Name property from the caption.
For example, if you give a menu item a Caption property value of File, C++Builder assigns the menu item a Name property of File1. If you fill in the Name property before filling in the Caption property, C++Builder leaves the Caption property blank until you type a value.
Note: If you enter characters in the Caption property that are not valid for C++ identifiers, C++Builder modifies the Name property accordingly. For example, if you want the caption to start with a number, C++Builder precedes the number with a character to derive the Name property.
The following table demonstrates some examples of this, assuming all menu items shown appear in the same menu bar.
|&File (2nd occurrence)
||Numerically orders duplicate items
||Adds a preceding letter and numerical order
|1234 (2nd occurrence)
||Adds a number to disambiguate the derived name
||Removes all non-standard characters, adding preceding letter and numerical order
||Numerical ordering of second occurrence of caption with no standard characters
As with the menu component, C++Builder adds any menu item names to the form’s type declaration, and those names then appear in the Component list.
Adding, inserting, and deleting menu items
The following procedures describe how to perform the basic tasks involved in building your menu structure. Each procedure assumes you have the Menu Designer window open.
To add menu items at design time,
- Select the position where you want to create the menu item.
If you’re just opened the Menu Designer, the first position on the menu bar is already selected.
- Begin typing to enter the caption. Or enter the Name property first by specifically placing your cursor in the Object Inspector and entering a value. In this case, you then need to reselect the Caption property and enter a value.
- Press Enter.
The next placeholder for a menu item is selected.
If you entered the Caption property first, use the arrow keys to return to the menu item you just entered. You’ll see that C++Builder has filled in the Name property based on the value you entered for the caption. (See Naming the menu items.)
- Continue entering values for the Name and Caption properties for each new item you want to create, or press Esc to return to the menu bar.
Use the arrow keys to move from the menu bar into the menu, and to then move between items in the list; press Enter to complete an action. To return to the menu bar, press Esc.
To insert a new, blank menu item,
- Place the cursor on a menu item.
- Press Ins.
Menu items are inserted to the left of the selected item on the menu bar, and above the selected item in the menu list.
To delete a menu item or command,
- Place the cursor on the menu item you want to delete.
- Press Del.
Note: You cannot delete the default placeholder that appears below the item last entered in a menu list, or next to the last item on the menu bar. This placeholder does not appear in your menu at runtime.
Separator bars insert a line between menu items. You can use separator bars to indicate groupings within the menu list, or simply to provide a visual break in a list.
To add a separator bar
- Add a menu item as described above and type a hyphen (-) for the caption.
To add accelerators or shortcuts to menu items
- see Specifying accelerator keys and keyboard shortcuts