Search code examples
menuc++builderc++builder-2007

Re-using a main menu item as popup menu


I have an application with a form that has a main menu. Now I would like to show a popup menu (MPopup) that contains an exact copy of the items in one of its sub-menus (MEdit):

class MyForm
{
__published:
TMainMenu *MainMenu1;
TMenuItem *MFile;
TMenuItem *MEdit;
// ...
TPopupMenu *MPopup;
// ...

How can I populate the popup menu without having to manage redundant items? All menus in question are static, no dynamic population.

System info: RAD Studio 2007 (C++ Builder)


Solution

  • You cannot share menu items between multiple menus. You have to make a copy of the items in each menu. In your Form's constructor, I would loop through the MainMenu items and create new PopupMenu sub-items that have the same property values, OnClick event handlers, etc as needed.

    UPDATE:

    Alternatively, you can place a TActionList on your Form and fill it with TAction objects that define your desired menu item properties and event handlers, and then you can create menu items in both menus as needed that simply refer to the same TActions via the TMenuItem::Action property.