Board index » delphi » How do I create a component dynamically?

How do I create a component dynamically?

Quote
Gareth Jones <[email protected]> wrote:

: Sorry to ask such a simple question - I'm pretty new to Delphi.

: I want to be able to create a control (a Tlabel for example), at a
: particular place on a form, at run-time. I can't figure out how to do
: this - any help would be much appreciated.

: regards,

: Gareth Jones

Hi,

    To create a visual component like TLabel, try:

var Label1: TLabel;
...
Label1 := TLabel.Create(self); {self means the main form}
Label1.Parent := self; {only needed for visual components}
Label1.SetBounds(left,top,width,height);
Label1.Caption := 'something';
.....

Hope this helps.

Regards,

William

 

Re:How do I create a component dynamically?


Quote
William Wong <[email protected]> wrote:
>Gareth Jones <[email protected]> wrote:

>: Sorry to ask such a simple question - I'm pretty new to Delphi.

>: I want to be able to create a control (a Tlabel for example), at a
>: particular place on a form, at run-time. I can't figure out how to do
>: this - any help would be much appreciated.

>: regards,

>: Gareth Jones

>Hi,

>    To create a visual component like TLabel, try:

>var Label1: TLabel;
>...
>Label1 := TLabel.Create(self); {self means the main form}
>Label1.Parent := self; {only needed for visual components}
>Label1.SetBounds(left,top,width,height);
>Label1.Caption := 'something';
>.....

>Hope this helps.

Thanks a lot  - it does. I wasn't setting the Parent property - I
assumed that the Create method did this for me. Now I see that the
create method specifies not the parent, but the owner of the
component...what is the difference between these?

TIA

Gareth

Re:How do I create a component dynamically?


Quote
William Wong <[email protected]> wrote:
>Gareth Jones <[email protected]> wrote:

>: Sorry to ask such a simple question - I'm pretty new to Delphi.

>: I want to be able to create a control (a Tlabel for example), at a
>: particular place on a form, at run-time. I can't figure out how to do
>: this - any help would be much appreciated.

>: regards,

>: Gareth Jones

>Hi,

>    To create a visual component like TLabel, try:

>var Label1: TLabel;
>...
>Label1 := TLabel.Create(self); {self means the main form}
>Label1.Parent := self; {only needed for visual components}
>Label1.SetBounds(left,top,width,height);
>Label1.Caption := 'something';
>.....

>Hope this helps.

Thanks a lot  - it does. I wasn't setting the Parent property - I
assumed that the Create method did this for me. Now I see that the
create method specifies not the parent, but the owner of the
component...what is the difference between these?

TIA

Gareth

Other Threads