Board index » delphi » Creating dynamically components

Creating dynamically components

Hello,

Can anybody help me on the following?
I would like to create a component at runtime. How do I do that?

Darius Blaszijk

 

Re:Creating dynamically components


Darius Blaszijk <DBlasz...@ZonNet.nl> schreef in berichtnieuws
RjAI5.24402$tL4.289095@zonnet-reader-1...

Quote
> Hello,

> Can anybody help me on the following?
> I would like to create a component at runtime. How do I do that?

> Darius Blaszijk

Darius: See example below for a TButton.

--
Regards,

Dirk Claessens
---------------------------------------------------------
http://www.claessens16.yucom.be
Attention: All spamshields raised; E-mails will bounce!
---------------------------------------------------------

{-------------------------------------------}
procedure TForm1.Button1Click(Sender: TObject);
var
  NewButton: TButton;
begin
  NewButton := TButton.Create( Self ); // self = the form
  with NewButton do
  begin
    Parent  := Self;  // ommit this, and it won't work...
    Left    := 100 + random(50);
    Top     := 100  + Random(50);
    Width   := 150;
    Height  := 25;
    Caption := 'Another New Button!' ;
    // I'm just lazy here <g> ...
    // any object method of the form
    // procedure TForm1.xyz ( Sender: TObject) will do!
    OnClick := Button1Click;
  end;
end;

Re:Creating dynamically components


Darius.

Open a new project and insert the following code.
This dynamically creates a button on form1 and adds an onclick event.  It's
basic but will get you started.

Steve

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
stdctrls;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    procedure Btnclick(Sender: TObject);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  with TButton.Create(self) do
  begin
    // put all the properties in here
    name := 'btnRun';
    caption := 'Push me';
    parent := Form1;
    left := 10;
    top := 10;
    onclick := BtnClick;
  end
end;

procedure TForm1.Btnclick(Sender: TObject);
begin
  if sender is TButton then
    TButton(Sender).Caption := 'Changed';
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Tbutton(FindComponent('BtnRun')).Free;   // we should destroy anything we
dynamically create
end;

end.

Quote
"Darius Blaszijk" <DBlasz...@ZonNet.nl> wrote in message

news:RjAI5.24402$tL4.289095@zonnet-reader-1...
Quote
> Hello,

> Can anybody help me on the following?
> I would like to create a component at runtime. How do I do that?

> Darius Blaszijk

Re:Creating dynamically components


Darius,

Start a new project and drop a button on it.
Add the component's unit to your uses-clause.
In the private-section for the form put :
  TestComp : TMyComp;

procedure TForm1.OnButton1Click(Sender : TObject);
begin
    TestComp := TMyComp.Create(Self);
    TestComp.Parent := Self;
    TestComp.Left := ...
    .....
    .....
    .....
end;

Alfred.

"Darius Blaszijk" <DBlasz...@ZonNet.nl> schreef in bericht
news:RjAI5.24402$tL4.289095@zonnet-reader-1...

Quote
> Hello,

> Can anybody help me on the following?
> I would like to create a component at runtime. How do I do that?

> Darius Blaszijk

Other Threads