Action: IActionItem;
The Action property determines the action that is associated with the menu item.
Determine one of the actions that is created in the ActionList component collection. On setting the action, properties of all properties that are set for the menu item will be ignored. Values of the corresponding properties of the selected action will be applied instead of them.
Executing the example requires a form with the button named Button1 on it, the MainMenu component named MainMenu1, the Toolbar component named Toolbar1 and the ActionList component named ActionList1.
Class TESTForm: Form
MainMenu1: MainMenu;
ActionList1: ActionList;
Toolbar1: Toolbar;
Button1: Button;
MenuItem1, MenuItem2: MenuItem;
TButton1, TButton2: ToolbarButton;
Action1, Action2: ActionItem;
Sub Button1OnClick(Sender: Object; Args: IMouseEventArgs);
Begin
//Create menu items
MenuItem1 := New MenuItem.Create;
MenuItem2 := New MenuItem.Create;
MainMenu1.Items.Add(MenuItem1);
MainMenu1.Items.Add(MenuItem2);
//Create buttons on the toolbar
TButton1 := New ToolbarButton.Create;
TButton2 := New ToolbarButton.Create;
Toolbar1.Controls.Add(TButton1);
Toolbar1.Controls.Add(TButton2);
//Create actions and their connection to menu items/buttons
Action1 := New ActionItem.Create;
Action1.Caption := "Open";
Action1.OnExecute := Action1OnExecute;
Action2 := New ActionItem.Create;
Action2.Caption := "Close";
Action2.OnExecute := Action2OnExecute;
ActionList1.AddAction(Action1);
ActionList1.AddAction(Action2);
MenuItem1.Action := Action1;
MenuItem2.Action := Action2;
TButton1.Action := Action1;
TButton2.Action := Action2;
End Sub Button1OnClick;
Sub Action1OnExecute(Sender: Object; Args: IEventArgs);
Begin
//The Action1 event handler
End Sub Action1OnExecute;
Sub Action2OnExecute(Sender: Object; Args: IEventArgs);
Begin
//The Action2 event handler
End Sub Action2OnExecute;
End Class TESTForm;
On clicking the button the following actions are executed:
Two menu items are created in the form menu.
Two buttons are created on the toolbar.
Two actions are created in the list of the ActionList1 component actions, the Caption base property is determined and various handlers of the OnExecute event are determined.
The created actions will be allocated for menu items and toolbar buttons. If menu item or button with the same text are selected, the same action is executed.
See also: