Totte Karlsson
Totte Karlsson

Reputation: 1341

Two TMemo objects sharing one TPopupMenu

I have a TForm with two TMemo objects and one TPopupMenu. Both TMemos are using the same TPopupMenu.

The popup menu has one item for 'clearing' the Memo, through the use of a TAction.

My question is, when clicking on the Menu item on one of the TMemos, how can I figure out which TMemo was the one that displayed the TPopupMenu?

I have the following code in the TAction's execute function:

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

TMenuItem* item = dynamic_cast<TMenuItem*>(action->ActionComponent);
if(item)
{
    if(dynamic_cast<TMemo*>(item->Owner))
    {
        dynamic_cast<TMemo*>(item->Owner) -> Clear();
    }
}

But the TMenuItem's owner is not a TMemo.

Any hints? I hope to avoid to have to use two different TPopupMenus.

Upvotes: 0

Views: 30

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 597941

You need to use the TPopupMenu::PopupComponent property to know which TMemo invoked the menu:

TMemo *memo = dynamic_cast<TMemo*>(PopupMenu1->PopupComponent);
if (memo)
    memo->Clear();

Or, if you want to discover the TPopupMenu that the TMenuItem is linked to:

TAction* action = static_cast<TAction*>(Sender);

TMenuItem* item = dynamic_cast<TMenuItem*>(action->ActionComponent);
if (item)
{
    TPopupMenu *popup = dynamic_cast<TPopupMenu*>(item->GetParentMenu());
    if (popup)
    {
        TMemo *memo = dynamic_cast<TMemo*>(popup->PopupComponent);
        if (memo)
            memo->Clear();
    }
}

Upvotes: 1

Related Questions