...create a Component at Runtime?
Author: Simon Grossenbacher
{
Dieser Code erzeugt zur Laufzeit einen Button namens "MyButton".
Beim Klicken auf diesen Button wird die Procedure 'ButtonClickHandler' ausgeführt.
This code create a button called "MyButton" at runtime.
Its onClick event is the procedure 'ButtonClickHandler'.
}
type
TForm1 = class(TForm)
{...}
private
MyButton: TButton;
// OnClick handler
procedure ButtonClickHandler(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
// OnClick handler
procedure TForm1.ButtonClickHandler(Sender: TObject);
begin
ShowMessage(TButton(Sender).Name);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
MyButton := TButton.Create(Self);
MyButton.Parent := Self; // Set its parent (Form1)
MyButton.Name := 'Button1';
MyButton.Caption := 'My Button';
MyButton.SetBounds(20, 20, 80, 40);
MyButton.OnClick := ButtonClickHandler; // assign onclick handler
end;
end.
printed from
www.swissdelphicenter.ch
developers knowledge base