Mega Code Archive

 
Categories / Delphi / Ide Indy
 

How to create menuitems at runtime

Title: How to create menuitems at runtime type TForm1 = class(TForm) Button1: TButton; label1: TLabel; MainMenu1: TMainMenu; Fonts1: TMenuItem; procedure Fonts1Click(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Fonts1Click(Sender: TObject); begin if Sender Fonts1 then label1.Caption := (Sender as TMenuItem).Caption; end; procedure TForm1.FormCreate(Sender: TObject); var NewItem: TMenuItem; i: Integer; begin for i := 0 to Screen.Fonts.Count - 1 do begin // Create a new Menu Item // Neuen Men¨¹punkt erzeugen NewItem := TMenuItem.Create(Self); // Take the Font name as Caption // Den Schriftnamen als Caption festlegen NewItem.Caption := Screen.Fonts.Strings[i]; // Assign a OnClick-Event // Ein OnClick-Ereignis zuweisen NewItem.OnClick := Fonts1Click; // Add the new menu // Den neuen Men¨¹punkt hinzuf¨¹gen Fonts1.Add(NewItem); end; end;