How do I programatically add actions to an Action Manager in Delphi 2010

会有一股神秘感。 提交于 2019-12-10 11:22:55

问题


I am trying to dynamically add actionitems, I can add the item and it works when I do this:

HostActionItem := ActionManager.ActionBars[0].Items[0].Items[2];
  NewItem := HostAction.Items.Add;
  NewItem.Action :=  MyActionToPerform;
  NewItem.Caption := Description;
  NewItem.ImageIndex := 1;
  NewItem.Tag := 13;

However, when the action Execute method fires I attempt to get the ActionComponent from the Sender object like this:

  if (Sender is TAction) then
  tag := (Sender As TAction).ActionComponent.Tag;

But the ActionComponent is always nil. Why is the ActionComponent not being initialised?


回答1:


short answer:

You're expecting a TActionClientItem to show up as ActionComponent of an TAction. That won't happen since TActionClientItem does not descend from TComponent.

longer answer:

I believe you're adding your item to a menu bar. It seems to be by design that an TAction linked to a menu item would not support the ActionComponent. The items of a menu bar is of type TActionClientItem. This is a 'collection item', not a 'component'. Hence the menu cannot fill in the ActionComponent parameter with the menu item when calling the Execute method of the action link of the selected item. If this sounds confusing, I guess the below quotes from the VCL source would make it clear:

TBasicActionLink.Execute method:

function Execute(AComponent: TComponent = nil): Boolean; virtual;

The passed component is assigned to FAction.ActionComponent before it is executed.

How it's called from TCustomActionMenuBar.ExecAction:

FSelectedItem.ActionLink.Execute;

For the question in the title, I don't think you're doing anything wrong, apart from setting the Caption and ImageIndex of a TActionClientItem is unnecessary, as it's the TAction's title and image which will be shown.



来源:https://stackoverflow.com/questions/4283491/how-do-i-programatically-add-actions-to-an-action-manager-in-delphi-2010

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!