Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: How can I find items in dynamically created Action Menus?


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 2 - Last Post: Dec 30, 2017 8:16 PM Last Post By: Errol Anderson
Errol Anderson

Posts: 2
Registered: 11/26/07
How can I find items in dynamically created Action Menus?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 21, 2017 1:06 AM
My program creates Action Menu items at runtime, with the caption of each item set to a value returned by a database query. The number of items and captions change with different databases, but the OnExecute procedure is the same for all items.

When I click on an item, I would like to obtain the item caption to use in another query in the OnExecute procedure. However, it seems that this is not possible in Delphi as Sender is the Action of the TActionClientItem, not the item itself.

This level of abstraction seems ridiculous and unnecessary, as it means that we have to use a mixture of popup menus if we require dynamic menu creation, and action menus for the unchanging menu items. Various forums suggest that many programmers would like to incorporate both types of menu in the same format. Surely it is not so difficult to return information about the menu item that has just been clicked, as well as the action. It seems that object-orientated purity has again trumped usefulness here. The Action Menu has been around for some years now and it is disappointing that a simple fix has not yet been implemented.

If there is some work-around, I would be grateful to hear of it.

Best regards

Errol
Navid Madani

Posts: 4
Registered: 4/21/12
Re: How can I find items in dynamically created Action Menus?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 29, 2017 6:56 PM   in response to: Errol Anderson in response to: Errol Anderson
Not sure what your setup is, VCL, FMX ...

Rather than setting the Action property, you could dynamically set the OnClick events of the individual dynamically created TMenuItem instances to a TNotifyEvent. The Sender parameter should then be the TMenuItem.

HTH,

Navid
Errol Anderson

Posts: 2
Registered: 11/26/07
Re: How can I find items in dynamically created Action Menus?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 30, 2017 8:16 PM   in response to: Errol Anderson in response to: Errol Anderson
I have received the following solution from another source, which I would like to share. This has been tested and works correctly, allowing access to item parameters such as Caption, etc.

================================================================
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.

==========================================================================

Errol Anderson wrote:
My program creates Action Menu items at runtime, with the caption of each item set to a value returned by a database query. The number of items and captions change with different databases, but the OnExecute procedure is the same for all items.

When I click on an item, I would like to obtain the item caption to use in another query in the OnExecute procedure. However, it seems that this is not possible in Delphi as Sender is the Action of the TActionClientItem, not the item itself.

This level of abstraction seems ridiculous and unnecessary, as it means that we have to use a mixture of popup menus if we require dynamic menu creation, and action menus for the unchanging menu items. Various forums suggest that many programmers would like to incorporate both types of menu in the same format. Surely it is not so difficult to return information about the menu item that has just been clicked, as well as the action. It seems that object-orientated purity has again trumped usefulness here. The Action Menu has been around for some years now and it is disappointing that a simple fix has not yet been implemented.

If there is some work-around, I would be grateful to hear of it.

Best regards

Errol
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02