Author: Jonas Bilinkevicius
I have a menu item in a menu and would like to fill a submenu with items loaded at
runtime.
Answer:
1 procedure TForm1.Button1Click(Sender: TObject);
2 var3 aItem, aSubItem: TMenuItem;
4 i, j: integer;
5 begin6 with MenuItem1 do{is an existing MenuItem}7 begin8 for i := 0 to 5 do9 begin10 aItem := TMenuItem.Create(MainMenu1);
11 Add(aItem);
12 aItem.Caption := IntToStr(i);
13 for j := 0 to 5 do14 begin15 aSubItem := TMenuItem.Create(MainMenu1);
16 aItem.Add(aSubItem);
17 aSubItem.Caption := IntToStr(j);
18 aSubItem.OnClick := MenuItemsHandler;
19 end;
20 end;
21 end;
22 end;
23 24 procedure TForm1.MenuItemsHandler(Sender: TObject);
25 begin26 ShowMessage((Sender as TMenuItem).Caption);
27 end;