procedure prcCreateMenuItems; var lMyMenuItem: TMenuItem; begin if not(lMyMainMenu = nil) then begin lMyMenuItem := TMenuItem.Create(lMyMainMenu); // try lMyMenuItem.Name := Format('MyMenuItem%d', [lMyMainMenu.Items.Count + 1]); lMyMenuItem.Caption := lMyMenuItem.Name; // lMyMainMenu.Items.Add(lMyMenuItem); except on E: Exception do { what to do? } ; end; end; end;
procedure TForm1.Button1Click(Sender: TObject); var i: integer; begin Button1.Enabled := False; // 2x NOT!!! // for i := 0 to 3 do prcCreateMenuItems; // if (lMyMainMenu.Items.Count > 0) then Self.Menu := lMyMainMenu; // show the menu on Form1 end;
procedure TForm1.Button2Click(Sender: TObject); begin // MainMenu.Items.Clear: // ---------- // Removes and frees all menu items listed in the Items property. // Use Clear to free all the items listed in the Items property array. // if not(lMyMainMenu = nil) then lMyMainMenu.Items.Clear; end;
in TMainMenu, "FItems" is a TMenuItem in TMenuItem, "FItems" is a TList
look, that "FItems" is created on "TMainMenu.Create" and never is deleted, because a MainMenu (to show itself) needs "Items"
And, the "Items" should have a "Caption" to appears on "MainMenu". Then, the "MainMneu appears on "Form"
Complex this logical!!!
No talk about "Assembly code" involved in this task! ----------
object MainMenu1: TMainMenu Left = 272 Top = 104 object TMenuItem // in Design-Time and with no Caption end end
function TMenu.CreateMenuItem: TMenuItem; begin Result := TMenuItem.Create(Self); end;
destructor TMenu.Destroy; begin FItems.Free; FImageChangeLink.Free; inherited Destroy; end;
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3