faqts : Computers : Programming : Languages : Delphi

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

18 of 23 people (78%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

Delphi: Component: Dynamic: Create: How to create dynamically a button component?

Sep 20th, 2004 13:18
hermann johannes, Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 11 September 2003 - 10:19 am ------------------

Delphi: Component: Dynamic: Create: How to create dynamically a button 
component (e.g. when clicking on another component)?

-----

You can use the 'Create' method of the 'TButton' class, with itself as
a parameter, to create a new button
(this is similar to using the 'new' method and 'this' parameter in
other computer languages).

-----

The minimum code looks similar to the following, in general:

procedure PROCProgramRunParallelBrowserOperaV7Parameter<some 
component>Click(Sender: TObject);

var <another componentname> : T<another component>;

begin

<another componentname> := T<another component>.Create(Self);

<some other componentname>.Parent := Self;

end;

---

The minimum code looks similar to the following, in specific:

Put e.g. a label on the form, double click on it, and put the following
in its OnClick event:

---

procedure TForm1.Label1Click(Sender: TObject);

var Btn : TButton;

begin

  Btn := TButton.Create( Self );

  Btn.Parent := Self;

end;

---

This will create a button component (it will just show the contours of
the button, and no caption)


 +-------------------+
 |                   |
 +-------------------+


 +-------------------+
 | Label1            |
 +-------------------+

---

Some more code added:

procedure TForm1.Label1Click(Sender: TObject);
var Btn : TButton;
begin
 Btn := TButton.Create( Self );
 Btn.Parent := Self;
 Btn.Left := 47;
 Btn.Top := 42;
 Btn.Width := Btn.Width + 25;
 Btn.Caption := 'My Button';
end;

---

When clicking on the label:

 +-------------------+
 | Label1            |
 +-------------------+

this will create a button component.

 +-------------------+
 | My Button         |
 +-------------------+

 +-------------------+
 | Label1            |
 +-------------------+

---
---

[Internet: see also:

---

Delphi: Can you give an overview of links of how to dynamically create 
new components?
http://www.faqts.com/knowledge_base/view.phtml/aid/31466/fid/175

----------------------------------------------------------------------