Building Application Menus
Go Up to How To Build VCL Forms Applications
Menus provide an easy way for your users to execute logically grouped commands. You can add or delete menu items, or drag them to rearrange them during designtime. In addition to TMainMenu and TPopupMenu components, the Tool Palette also contains TActionMainMenuBar, TActionManager, and TActionToolBar.
To create application menus
- Choose File > New > Other .The New Items dialog appears.
- In the New Items dialog, select VCL Forms Application.The Windows Designer displays.
- Build application menus.
- Use the Menu Designer.
- Create an event handler for each menu item.
- Move menu items.
- Add images to menu items.
To build application menus
- From the Standard category of the Tool Palette, add TMainMenu or TPopupMenu component to your form.A visual representation of the menu appears on the designer.
Note: A TMainMenu component creates a menu that is attached to the title bar of the form. A TPopupMenu component creates a menu that appears when the user right-clicks in the form.
- To view the menu, if the form is visible, click the form.The menu appears in the form exactly as it will when you run the program.
- To delete a menu item, select the menu item you want to delete. Press Delete.
- To edit menu items, select the VCL form, select the menu item you want to edit, and edit its properties.
- To make the menu item a separator bar, place the cursor on the menu where you want a separator to appear and enter a hyphen (-) for the caption or press the hyphen (-) key.
- To specify a keyboard shortcut for a menu item, in the Object Inspector, set the ShortCut property.
To use the Menu Designer
- Select a menu component on the form.
- Double-click the menu component.The Menu Designer window opens.
Note: You can also open the Menu Designer by clicking the ellipsis(...) button next to the Items property in the Object Inspector.
- To name a menu component, in the Object Inspector, set the Caption property.
Tip: Delphi derives the Name property from the caption, for e.g. if you give a menu item a Caption property value of File, Delphi assigns the menu item a Name property of File1. However, if you fill in the Name property before filling in the Caption property, Delphi leaves the Caption property blank until you type a value.
- Right-click anywhere on the Menu Designer to use the Menu Designer context menu.A drop-down list opens. This is the Menu Designer context menu.
- To insert a placeholder below or to the right of the cursor, choose Insert from the context menu.
- To delete the selected menu item (and all its subitems, if any), click Delete from the context menu.
- To switch among menus in a form, choose Select Menu from the context menu.The Select Menu dialog box appears. It lists all the menus associated with the form whose menu is currently open in the Menu designer.
- From the list in the Select Menu dialog box, choose the menu you want to view or edit.
To create an event handler for a menu item
- In the designer, double-click the menu item to which you wish to add an event handler.The Code Designer appears, cursor in place between event handler brackets.
- Code your menu item logic.
- Save and compile the application.
To move menu items
- To move a menu item along the desired menu bar, drag the item until the arrow tip of the cursor points to the new location.
- Release the mouse button.
- To move a menu item into a menu list, drag the item until the arrow tip of the cursor points to the new menu.
- Release the mouse button.
To add images to menu items
- From the Tool Palette, drag a TMainMenu or TPopupMenu component to a form.
- From the Tool Palette, drop a TImageList component to the form.
- Double-click on the TImageList component.The ImageList editor opens.
- Click Add to select the bitmap or bitmap group you want to use in the menu.
- Select the bitmap that you want and click OK.
- In the Object Inspector, set the Images property of the TMainMenu or TPopupMenu component to the image you selected in the ImageList editor.