0

I have a number of related TMenuItems, in a CodeGear C++ VCL application.

Each TMenuItem is associated to the same action (TAction).

When a MenuItem is clicked, the action fires (its execute method that is).

I will need to, somehow, cast the Sender parameter in the actions OnExecute function to figure out which menuitem that was clicked.

Currently I have something like this

void __fastcall TMoleculixDesktopMainUnit::openMoleculeSelectionFormAExecute(TObject *Sender)
{
//User selected a menuitem under Molecules Menu

TAction* anItem = dynamic_cast<TAction*>(Sender);


//AminoAcidsMI is a TMenuItem
if(AminoAcidsMI == dynamic_cast<TMenuItem*>(anItem->Owner))
{
    //Open molecule search form with aminoacids
    MLog()<<"Looking for Amino Acids..";
}
}

But the above does not work The actions Owner is NOT the MenuItem.

Niall
  • 30,036
  • 10
  • 99
  • 142
Totte Karlsson
  • 1,261
  • 1
  • 20
  • 55
  • Your `TAction.Execute` should do one thing - it should handle the code that deals with the `AminoAcidsMI` and any other controls that perform the same functionality. Then it doesn't matter which one of them is used, as they would all reach the same execution point. (Actions are designed to be attached to multiple controls such as menu items, toolbar buttons, popup menus, and so forth, so no single control could possibly "own" the action.) – Ken White Oct 05 '14 at 20:34

1 Answers1

2

Use the TAction::ActionComponent property, which specifies the component that triggered the action.

void __fastcall TMoleculixDesktopMainUnit::openMoleculeSelectionFormAExecute(TObject *Sender)
{
    //User selected a menuitem under Molecules Menu

    TAction* anItem = dynamic_cast<TAction*>(Sender);
    if (!anItem) return;

    AminoAcidsMI == dynamic_cast<TMenuItem*>(anItem->ActionComponent);
    if (AminoAcidsMI)
    {
        //Open molecule search form with aminoacids
        MLog()<<"Looking for Amino Acids..";
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770