Mega Code Archive

 
Categories / Delphi / Ide Indy
 

Create a Component at Runtime

Title: Create a component at runtime Question: How to use the create-constructor and the owner and parent properties to create a comoponent at runtime. Answer: When you create visual controls at runtime it's just important to assign the parent property and to use the method SetBounds, so that the control will be visible. type TForm1 = class(TForm) protected MyLabel: TLabel; procedure LabelClick(Sender: TObject); procedure CreateControl; end; procedure TForm1.LabelClick(Sender: TObject); begin (Sender as Label).Caption := ... end; procedure TForm1.CreateControl; var ALeft, ATop, AWidth, AHeight: Integer; begin ALeft := 10; ATop := 10; AWidth := 50; AHeight := 13; MyLabel := TLabel.Create(Self); MyLabel.Parent := Self; MyLabel.Name:='LabelName'; MyLabel.SetBounds(ALeft, ATop, AWidth, AHeight); MyLabel.OnClick := LabelClick; end;