Home>>Programming>>C++Builder>> » How to Build Menus in Delphi or C Builder

How to Build Menus in Delphi or C Builder

Last Updated on August 1, 2018 By hcyaobin

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

  1. Naming menus
  2. Naming the menu items
  3. Adding, inserting, and deleting menu items
  4. Creating submenus
  5. Adding images to menu items
  6. Viewing the menu

Naming menus

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.

Component caption Derived name Explanation
&File File1 Removes ampersand
&File (2nd occurrence) File2 Numerically orders duplicate items
1234 N12341 Adds a preceding letter and numerical order
1234 (2nd occurrence) N12342 Adds a number to disambiguate the derived name
$@@@# N1 Removes all non-standard characters, adding preceding letter and numerical order
– (hyphen) N2 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,

  1. 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.
  2. 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.
  3. 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.)
  4. 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,

  1. Place the cursor on a menu item.
  2. 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,

  1. Place the cursor on the menu item you want to delete.
  2. 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

Add a Comment

Your email address will not be published. Required fields are marked *