Associating Menu Events with Event Handlers
Go Up to Working with Events and Event Handlers
The Menu Designer, along with the MainMenu and PopupMenu components, make it easy to supply your application with drop-down and pop-up menus. For the menus to work, however, each menu item must respond to the OnClick event, which occurs whenever the user chooses the menu item or presses its accelerator or shortcut key. This topic explains how to associate event handlers with menu items. For information about the Menu Designer and related components, see Creating and Managing Menus.
To create an event handler for a menu item
- Open the Menu Designer by double-clicking a Vcl.Menus.TMainMenu or Vcl.Controls.TControl.PopupMenu component in the Form Designer.
- Select a menu item in the Menu Designer.
- In the Object Inspector, make sure that a value is assigned to the item's Name property.
- From the Menu Designer, double-click the menu item. The Code Editor opens with the cursor inside the skeleton event handler, or the begin...end block.
- At the cursor, type the code that you want to execute when the user selects the menu command.
To associate a menu item with an existing OnClick event handler
- Open the Menu Designer by double-clicking a MainMenu or PopupMenu component.
- Select a menu item in the Menu Designer. In the Object Inspector, make sure that a value is assigned to the item's Name property.
- On the Events page of the Object Inspector, click the down arrow in the Value column next to OnClick to open a list of previously written event handlers. (The list includes only event handlers written for OnClick events on this form.) Select from the list by clicking an event handler name.