delphimenutoolbartactiontactionmanager

How to assign TAction to toolbar button at runtime?


I add toolbar buttons dynamically based on settings file. I would like to assign TAction to them. For now I added OnClick event with action OnExecute handler but if action is disabled, then toolbar button is not disabled automatically like for example TMenuItems added at design time.

So, in other words, I'd like to have runtime version of design time assigning TAction using "Action" dropdown menu in IDE.

Here is what I did so far (the code is C++ but it's obvious what it does).

TToolButton *b;
b = new TToolButton(ToolBar1);
b->Action = Form1->ActionManager1->Actions[0];
b->Parent = ToolBar1;

This adds button to ToolBar and it seems to follow action enabled/disabled state but when clicked nothing happens. How do I add action properly and fully?


Solution

  • TToolButton has a published Action property. Simply assign your desired TAction object to that property.