Board index » cppbuilder » Creating an instance of a control at runtime

Creating an instance of a control at runtime

Hi All,

I'm sure this is a tired question, but I'm having a problem creating an
instance of a component at runtime.  This is what I do:

    abutton = new TButton(Form1);
    abutton->Top = 10;
    abutton->Left = 10;
    abutton->Width = 50;
    abutton->Height = 50;

abutton is a member of TForm1, the main form's class.  I first create the
object and set its parent as the form I want to place it on.  Then I set
appropriate values for the position and size metrics to put it up in the
left hand corner.  The only problem I am having with this is that the button
just doesn't show up.  I've also tried this with my own custom components,
and I've tried using a TPanel as the parent, but to no avail.  I'm sure I'm
just missing something simple here, can anyone tell me what?

Thanks,

-Mike Powers

 

Re:Creating an instance of a control at runtime


Run the following code in the form constructor.
and declare abutton in the header file (I use the private section);

You must specifically  set the Parent property or it won't work.

Don't forget to delete in the form's destructor.

Jim Bailey
TeamB

    abutton = new TButton(this);
    abutton->Parent = this;
    abutton->Top = 100;
    abutton->Left = 100;
    abutton->Width = 75;
    abutton->Height = 75;

Quote
"Michael A. Powers" wrote:
> Hi All,

> I'm sure this is a tired question, but I'm having a problem creating an
> instance of a component at runtime.  This is what I do:

>     abutton = new TButton(Form1);
>     abutton->Top = 10;
>     abutton->Left = 10;
>     abutton->Width = 50;
>     abutton->Height = 50;

> abutton is a member of TForm1, the main form's class.  I first create the
> object and set its parent as the form I want to place it on.  Then I set
> appropriate values for the position and size metrics to put it up in the
> left hand corner.  The only problem I am having with this is that the button
> just doesn't show up.  I've also tried this with my own custom components,
> and I've tried using a TPanel as the parent, but to no avail.  I'm sure I'm
> just missing something simple here, can anyone tell me what?

> Thanks,

> -Mike Powers

Re:Creating an instance of a control at runtime


Quote
Jim Bailey(TeamB) wrote:
> Don't forget to delete in the form's destructor.

But Jim...

...if the TButton constructor takes a TComponent *AOwner
parameter, won't the form delete it?

The help for TComponent::Owner says:
"Indicates the component that is responsible for freeing this component."

Now...
...can you please explain what is going on concerning the following:

ShowMessage(Form1->Name);
The Form1 variable that is declared in the form's cpp file by the
new application expert points to a class called Form1_1.

ShowMessage(String(Form1->ClassName()));
Shows that the Class name is TForm1.

Now this is the really wierd part.  If I do this:
Form1->Show();

I have *two* TForm1's staring me in the face.  I haven't been smoking
anything and pinching myself really hard doesn't seem to wake me up.
There seems to be a secret, hidden alternate dimension, slightly out of
phase, which is slowly intersecting with ours...

I'm looking forward to other explanations.

FF

Re:Creating an instance of a control at runtime


OK, there seem to be two TForm1's in the auto-create
 forms list.  How did that get there?!

Doo doo doo doo, doo doo doo doo...

Re:Creating an instance of a control at runtime


Ok, I deleted the second TForm1 from the auto-create list
and got an access violation when I closed the Project Options
dialog.  I  opened it again and the second TForm1 was still
gone.  Now Michael's code works, with the Parent property
set as he stated.  Maybe Michael should check his Project
Options for the same duplicate form problem.  Could be
some flaky IDE bug?

Other Threads