0

My program creates Action Menu items at runtime, with the caption of each item set to a value returned by a database query.

The OnExecute procedure is the same for all items.

When I click on an item, I want to obtain the item caption to use in the OnExecute procedure.

I have tried the following (analogous to TMenuItem for a popup menu):

sCaption := TActionClientItem(Sender).Caption;

However, this always returns an empty string.

I have searched the web and the Delphi Help to no avail. I am aware that Caption is a published property, but the public property Index also fails.

I presume I am doing something wrong here, but cannot figure out what.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770

2 Answers2

0

The information provided is not so detailed, but I guess the OnExecute procedure that you are describing is the event from a TAction which is included in a TActionManager.

The problem is that the Sender of the OnExecute event is the TAction that it is just clicked! So when you are casting the TActionClientItem to the Sender

TActionClientItem(Sender)

it gives you nothing.

Quick solution would be first to create the TAction(s), give them the caption you wish and then assign those TActions to your TActionClientItem(s). They will get automatically the Caption of the assigned Actions.

actionClientItem1.Action := Action1;

You get then your caption on the event

sCaption := TAction(Sender).Caption;
MaGiC
  • 311
  • 1
  • 4
0

The problem with the suggested answer of creating an Action for each item is that I wish to use the same action for many items (the number determined by a database query count). The action is then modified by the value of the caption, being the value of each record of the database query.

However, the following answer has been suggested to me. I have tested this and it works:

"If the items are on the TActionMainMenuBar component then you can do the following:

type
  TCustomActionMenuBarAccess = class(TCustomActionMenuBar);

In the Action Execute you can then do:

  if TCustomActionMenuBarAccess(ActionMainMenuBar1).FSelectedItem <> nil then
    ShowMessage('Selected Item: ' + TCustomActionMenuBarAccess(ActionMainMenuBar1).FSelectedItem.Caption);

where ActionMainMenuBar1 is the name of the TActionMainMenuBar component on the form.

If the items are created on a toolbar, then you can use the TAction(Sender).ActionComponent property as the TAction is the sender in these cases."