Board index » delphi » Instantiating Components at runtime

Instantiating Components at runtime

I need to be able to create components at runtime. For instance, I want
the user to be able to create many objects on a window, therefore, there
will be a different number of objects each time the program is run.

I have been attempting this for some time now, but have had one problem.
Once the new object is created, it will not be displayed.

I have tried the following:

var
   myObject: TButton;
begin
   myObject := TButton.Create(Button1); { Button1 is a button I drew on
                                          the window }

I then set all the position and size properties, and called the Show
method. When I do a watch on myObject, I can see that it is no longer set
to nil, but it will not appear on the window. It is very frustrating!

Can anyone help.

Thanking you in advance.

 

Re:Instantiating Components at runtime


Quote
stri...@gondor.mv.com (Yourslave) wrote:

[snip]

Quote
>I have tried the following:
>var
>   myObject: TButton;
>begin
>   myObject := TButton.Create(Button1); { Button1 is a button I drew on
>                                          the window }

Add the line

      myObject.Parent := Main_Form {or whatever the form name is}

That should do it...

Jon

JonL...@ix.netcom.com

Re:Instantiating Components at runtime


Quote
stri...@gondor.mv.com (Yourslave) writes:

[snip]B

Quote
>I have tried the following:
>var
>   myObject: TButton;
>begin
>   myObject := TButton.Create(Button1); { Button1 is a button I drew on
>                                          the window }
>I then set all the position and size properties, and called the Show
>method. When I do a watch on myObject, I can see that it is no longer set
>to nil, but it will not appear on the window. It is very frustrating!

        myObject.Parent := myForm;

This will make your control appear as a child of the form, or use some
other container object if desired.

Luke
--
Luke Webber

* Note: The opinions expressed by Luke Webber are in no way supported *
*       by his employers, Luke Webber Consulting Services             *

Re:Instantiating Components at runtime


Quote
stri...@gondor.mv.com (Yourslave) wrote:
>I need to be able to create components at runtime. For instance, I want
[Hrum...Hrum]
>I have tried the following:

>var
>   myObject: TButton;
>begin
>   myObject := TButton.Create(Button1); { Button1 is a button I drew on
>                                          the window }

I had the same problem:

  When I create a component at run-time I should do such thing:

MyComp:=TComp.Create(Owner);

Ok. For Example: I want to create it on the form FORM1:

I try MyComp:=TComp.Create(FORM1)! It does not work...

But lines

MyComp:=TComp.Create(nil);
FORM1.InsertComponent(MyComp);

work properly...

Try do this.

Sincerly, Kostya.

Re:Instantiating Components at runtime


"Konstantin E. Belov" <kos...@mailserv.tversu.ac.ru> wrote:

Quote
>stri...@gondor.mv.com (Yourslave) wrote:
>>I need to be able to create components at runtime. For instance, I want
>[Hrum...Hrum]
>>I have tried the following:

>>var
>>   myObject: TButton;
>>begin
>>   myObject := TButton.Create(Button1); { Button1 is a button I drew on
>>                                          the window }

>I had the same problem:
>  When I create a component at run-time I should do such thing:
>MyComp:=TComp.Create(Owner);
>Ok. For Example: I want to create it on the form FORM1:
>I try MyComp:=TComp.Create(FORM1)! It does not work...
>But lines
>MyComp:=TComp.Create(nil);
>FORM1.InsertComponent(MyComp);

The absolute first thing you should do after you create a visible
component dynamically (if you want it to show up) is to set the Parent
property.

Thus,

MyComp := TComp.Create(nil);
MyComp.Parent := Self;

where the code is called from an eventhandler or procedure on a form.

HTH,
Bert

Other Threads