2

When I create a TmenuItem programmatically the usual way, the Owner of the created menu item is passed as a parameter in the Create function, e.g.:

var NewMenuItem := TMenuItem.Create(MainMen1);

However, when creating a menu item by CLONING it, assigning its Owner property causes an error:

function CloneMenuItem(SourceItem: TMenuItem): TMenuItem;
begin
  with SourceItem do  
    Result := Vcl.Menus.NewItem(Caption, Shortcut, Checked, Enabled, OnClick, HelpContext, Name + 'Cloned'); 
  //Result.Owner := pmMyPopupMenu; // ERROR
end;

So how can I specify the Owner of a cloned TMenuItem?

user1580348
  • 5,721
  • 4
  • 43
  • 105
  • 1
    You can clone a `TMenuItem` by means of `TMenuItem.Create`. Just create it with your desired owner and then set its `Caption`, `Shortcut`, `Checked`, etc. properties. – Andreas Rejbrand Jun 07 '22 at 11:49
  • `Vcl.Menus.NewItem` parameters for `Hint` and `Break` properties are MISSING. Did Embarcadero forget to include these properties in `Vcl.Menus.NewItem` parameters? – user1580348 Jun 09 '22 at 09:07

1 Answers1

3

You can set the owner using TComponent.InsertComponent method like:.

function CloneMenuItem(SourceItem: TMenuItem): TMenuItem;
begin
  with SourceItem do  
    Result := Vcl.Menus.NewItem(Caption, Shortcut, Checked, Enabled, OnClick, HelpContext, Name + 'Cloned'); 
  pmMyPopupMenu.InsertComponent(Result);
  pmMyPopupMenu.Items.Add(Result);
end;

If you look at NewItem function implementation in Vcl.Menus, it simply creates an item with nil as owner and sets the passed properties. Nothing smart there. I'd prefer inline code in your case or a local function that explicitly sets the owner on item creation.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Marcodor
  • 4,578
  • 1
  • 20
  • 24