BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How do I dynamically create a VCL component

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage)
View previous topic :: View next topic  
Author Message
William Riley
Guest





PostPosted: Fri Sep 15, 2006 12:36 am    Post subject: How do I dynamically create a VCL component Reply with quote



Builders,

I am trying to dynamically create a VCL component and cannot seem to get it
to work. The code compiles and seems to run fine but I cannot see the
component on the form.
The Borland C++ Builder Development Guide seems to indicate that this can be
done but is short on details. My understanding of C++ Builder get to be more
every day but certainly there is something I do not understand about this
and maybe doing something stupid; I suspect it may have something to do with
the Component Owner parameter which I have set to this (Form1). I have
included the code below.
Any help, comment and suggestions appreciated.

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "dynamicComponentTest.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void
__fastcall TForm1::OnCreateFormEventHandler(TObject *Sender)
{
dynamicTEditComponent = new TEdit(this);
}
//---------------------------------------------------------------------------
void
__fastcall TForm1::OnCloseFormEventHandler(TObject *Sender, TCloseAction
&Action)
{
delete dynamicTEditComponent;
}
//---------------------------------------------------------------------------
void
__fastcall TForm1::OnPaintFormEventHandler(TObject *Sender)
{
dynamicTEditComponent->Top = ClientHeight/2;
dynamicTEditComponent->Left = ClientWidth/2;
dynamicTEditComponent->Height = 20;
dynamicTEditComponent->Width = 80;
// Make sure it we can see it if it is painted to form
dynamicTEditComponent->Color = clRed;
dynamicTEditComponent->Text = "Dynamic TEdit Component";
dynamicTEditComponent->Show();
dynamicTEditComponent->Repaint(); // but I can't see it
}
//---------------------------------------------------------------------------
Back to top
JD
Guest





PostPosted: Fri Sep 15, 2006 1:01 am    Post subject: Re: How do I dynamically create a VCL component Reply with quote



"William Riley" <William_R1 (AT) verifone (DOT) com> wrote:
Quote:


You never set it's Parent property.

~ JD
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Fri Sep 15, 2006 1:04 am    Post subject: Re: How do I dynamically create a VCL component Reply with quote



"William Riley" <William_R1 (AT) verifone (DOT) com> wrote in message
news:4509af4a$1 (AT) newsgroups (DOT) borland.com...

Quote:
I am trying to dynamically create a VCL component and cannot seem
to get it to work. The code compiles and seems to run fine but I cannot
see the component on the form.

That is because you are making a classic newbie mistake - you are not
setting the Parent property.

Quote:
I suspect it may have something to do with the Component Owner
parameter which I have set to this (Form1).

The Owner has nothing to do with the component's visibility. The Owner
manages the component's memory, nothing else. The Parent is who controls
where the component is positioned visually, and whether the component can be
interested with.

Quote:
void
__fastcall TForm1::OnCreateFormEventHandler(TObject *Sender)

Do not use the OnCreate event in C++. It is a Delphi idiom that produces
illegal behavior in C++, as it can be triggered before the constructor. Use
the actual constructor instead.

The same applies to the OnDestroy event. It can be triggered after the
destructor, which is also illegal in C++. Use the actual destructor
instead.

Quote:
void
__fastcall TForm1::OnCloseFormEventHandler(TObject *Sender, TCloseAction
&Action)
{
delete dynamicTEditComponent;
}

Strictly speaking, you do not need to delete Owned components. They are
automatically freed when their Owner is freed.

Quote:
void
__fastcall TForm1::OnPaintFormEventHandler(TObject *Sender)

Do not use the OnPaint event like that. You should be setting the TEdit's
properties at the time that you create it, not every time the form is
re-painted (the OnPaint event is triggered many times during the form's
lifetime):

__fastcall TForm1::TForm1(TComponent *Owner)
: TForm(Owner)
{
dynamicTEditComponent = new TEdit(this);
dynamicTEditComponent->Parent = this; // <-- add this!!
dynamicTEditComponent->SetBounds(ClientWidth/2, ClientHeight/2, 80,
20);
dynamicTEditComponent->Color = clRed;
dynamicTEditComponent->Text = "Dynamic TEdit Component";
}


Gambit
Back to top
William Riley
Guest





PostPosted: Fri Sep 15, 2006 2:12 am    Post subject: Re: How do I dynamically create a VCL component Reply with quote

Thanks for the info and helpful tips. I moved the dynamic TEdit creation to
form constructor and set the control Parent property to this and things
appear to be working fine. I agree about the setting of TEdit properties in
OnPaint event but wanted to make sure something was not getting reset behind
my back until I could figure out what was going on.
BTW any suggestions for reference material I can find or buy to better
understand the Borland C++ Builder. There are only a couple books I have
found and the one Borland C++ Builder Developer Guide published by H SAMS
(not sure of the exact title) is not all that enlightening. It basically
makes me aware that certain features exist but I end up plowing through the
C++ Builder help files and trying a lot of code; not very efficient.

"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote in message
news:4509b65e$1 (AT) newsgroups (DOT) borland.com...
Quote:

"William Riley" <William_R1 (AT) verifone (DOT) com> wrote in message
news:4509af4a$1 (AT) newsgroups (DOT) borland.com...

I am trying to dynamically create a VCL component and cannot seem
to get it to work. The code compiles and seems to run fine but I cannot
see the component on the form.

That is because you are making a classic newbie mistake - you are not
setting the Parent property.

I suspect it may have something to do with the Component Owner
parameter which I have set to this (Form1).

The Owner has nothing to do with the component's visibility. The Owner
manages the component's memory, nothing else. The Parent is who controls
where the component is positioned visually, and whether the component can
be
interested with.

void
__fastcall TForm1::OnCreateFormEventHandler(TObject *Sender)

Do not use the OnCreate event in C++. It is a Delphi idiom that produces
illegal behavior in C++, as it can be triggered before the constructor.
Use
the actual constructor instead.

The same applies to the OnDestroy event. It can be triggered after the
destructor, which is also illegal in C++. Use the actual destructor
instead.

void
__fastcall TForm1::OnCloseFormEventHandler(TObject *Sender, TCloseAction
&Action)
{
delete dynamicTEditComponent;
}

Strictly speaking, you do not need to delete Owned components. They are
automatically freed when their Owner is freed.

void
__fastcall TForm1::OnPaintFormEventHandler(TObject *Sender)

Do not use the OnPaint event like that. You should be setting the TEdit's
properties at the time that you create it, not every time the form is
re-painted (the OnPaint event is triggered many times during the form's
lifetime):

__fastcall TForm1::TForm1(TComponent *Owner)
: TForm(Owner)
{
dynamicTEditComponent = new TEdit(this);
dynamicTEditComponent->Parent = this; // <-- add this!!
dynamicTEditComponent->SetBounds(ClientWidth/2, ClientHeight/2, 80,
20);
dynamicTEditComponent->Color = clRed;
dynamicTEditComponent->Text = "Dynamic TEdit Component";
}


Gambit

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.