Author: Jonas Bilinkevicius
How do you attach a popup menu to the TMenuItem of a main menu as a submenu at
runtime?
Answer:
This is one way to do it. Note: The PopupMenuItems are added to an existing menu
item of the mainmenu mnuWhatEver.
1 { ... }2 var3 I: integer;
4 MenuItem: TMenuItem;
5 begin6 for I := 0 to PopupMenu1.Items.Count - 1 do7 begin8 with PopupMenu1.Items[I] do9 MenuItem := NewItem(Caption, ShortCut, Checked, Enabled, OnClick, HelpContext,
10 Name);
11 mnuWhatEver.Add(MenuItem);
12 end;
13 end;
Note: when using NewItem the MenuItem has no owner you'll have to free them
yourself when you're done with it.
You could also use:
14 MenuItem := TMenuItem.Create(Self);
15 16 //and then copy all the properties yourself.17 18 MenuItem.Captions := PopupMenu1.Items[I].Caption;
19 MenuItem.OnClick := PopupMenu1.Items[I].OnClick;
20 {etc.}