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 

Can a component dropped on a from delete itself?

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





PostPosted: Thu Feb 15, 2007 1:41 am    Post subject: Can a component dropped on a from delete itself? Reply with quote



I am writing a TCustomControl descendant and I am able to drop it on a form.

The Goal:

This descendant will look for any other descendants of its type on its
parents Control list and delete itself if one already exists.

I tried Owner->RemoveComponent( this ) and it appears to work but the
component seems to still be drawing itself on the form. I added
Parent->RemoveControl but this causes exceptions.

The Question:

How can a TCustomControl descendant remove itself from the form at design
time?
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Thu Feb 15, 2007 1:50 am    Post subject: Re: Can a component dropped on a from delete itself? Reply with quote



"Larry Griffiths" <nospame (AT) nowhere (DOT) com> wrote in message
news:45d36690$1 (AT) newsgroups (DOT) borland.com...

Quote:
This descendant will look for any other descendants of its type on
its
parents Control list and delete itself if one already exists.

Do not delete it at all. Inside the constructor, throw an exception
instead. That will abort the construction.

Quote:
I tried Owner->RemoveComponent( this ) and it appears to work but
the
component seems to still be drawing itself on the form.

As it should be. You did not actually delete the component. All you
did was remove its memory management from the Owner's list. Besides,
removing it from the Owner is not the right thing to do anyway. What
if the user creates the component dynamically at runtime? There may
not be an Owner assigned at all.


Gambit
Back to top
Larry Griffiths
Guest





PostPosted: Thu Feb 15, 2007 8:01 pm    Post subject: Re: Can a component dropped on a from delete itself? Reply with quote



Thanks Gambit,

What I had come up with was the following but I think the Parent Propery is
filled in when it goes through the constructer. and may be the better route
to go...

void __fastcall TSomeClass::ValidateContainer(TComponent* AComponent)
{
// The first time this is called, AComponent is Owner.
// The Second time this method is called, AComponent is Parent.

if ( ValidateContainerCount++ )
{
TWinControl *WC = dynamic_cast<TWinControl*>( AComponent );

if( WC )
{
for ( int ix = 0; ix < WC->ControlCount; ix++ )
{
if( TSomeClass already in Parents list )
{
// Do not allow the component to be added to the form.
throw Exception( Message );
}



"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote in message
news:45d36804 (AT) newsgroups (DOT) borland.com...
Quote:

"Larry Griffiths" <nospame (AT) nowhere (DOT) com> wrote in message
news:45d36690$1 (AT) newsgroups (DOT) borland.com...

This descendant will look for any other descendants of its type on
its
parents Control list and delete itself if one already exists.

Do not delete it at all. Inside the constructor, throw an exception
instead. That will abort the construction.

I tried Owner->RemoveComponent( this ) and it appears to work but
the
component seems to still be drawing itself on the form.

As it should be. You did not actually delete the component. All you
did was remove its memory management from the Owner's list. Besides,
removing it from the Owner is not the right thing to do anyway. What
if the user creates the component dynamically at runtime? There may
not be an Owner assigned at all.


Gambit

Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Fri Feb 16, 2007 12:07 am    Post subject: Re: Can a component dropped on a from delete itself? Reply with quote

"Larry Griffiths" <nospame (AT) nowhere (DOT) com> wrote in message
news:45d46849$1 (AT) newsgroups (DOT) borland.com...

Quote:
What I had come up with was the following but I think the
Parent Propery is filled in when it goes through the constructer.

The Parent property is not set until after the constructor has exited.

Quote:
may be the better route to go...

For design-time created instance, the Owner that is passed to the
constructor will be the same control that will be assigned as the
Parent, so you can validate the Owner immediately. For run-time
created instances, however, you will have to wait for the Parent
property to be set, since the user could pass a NULL Owner, or a
different Owner than the Parent.

Try the following (untested):

class TSomeClass : public TCustomControl
{
typedef TCustomControl inherited;
private:
bool FHasInitialOwner;
bool FSettingParent;
protected:
virtual void __fastcall SetParent(TWinControl *AParent);
virtual void __fastcall ValidateContainer(TComponent*
AComponent);
public:
__fastcall TSomeClass(TComponent *Owner);
};

__fastcall TSomeClass::TSomeClass(TComponent *Owner)
: TCustomControl(Owner)
{
FHasInitialOwner = true;
//...
}

void __fastcall TSomeClass::SetParent(TWinControl *AParent)
{
FSettingParent = true;
try
{
inherited::SetParent(AParent);
}
__finally {
FSettingParent = false;
}
}

void __fastcall TSomeClass::ValidateContainer(TComponent*
AComponent)
{
if( ((AComponent->ComponentState.Contains(csDesigning)) &&
(!FHasInitialOwner))
|| FSettingParent )
{
TWinControl *WC = dynamic_cast<TWinControl*>(AComponent);
if( WC )
{
for( int ix = 0; ix < WC->ControlCount; ++ix)
{
if( dynamic_cast<TSomeClass*>(WC->Controls[ix] )
{
// Do not allow the component to be added to
the form.
throw Exception("TSomeClass already exists");
}
}
}
}
}


Gambit
Back to top
JD
Guest





PostPosted: Sun Feb 18, 2007 9:18 pm    Post subject: Re: Can a component dropped on a from delete itself? Reply with quote

"Larry Griffiths" <nospame (AT) nowhere (DOT) com> wrote:
Quote:

[...] This descendant will look for any other descendants of
its type on its parents Control list and delete itself if
one already exists.

It sounds as if you want only one copy of that control
and if so, there is a much easier way than itterating all
controls: Just use a global pointer. For example:

TSingleInstance *ThisSingleInstance = NULL;
//-------------------------------------------------------------
__fastcall TSingleInstance::TSingleInstance(TComponent* Owner) : TComponent(Owner)
{
if( ThisSingleInstance )
{
throw Exception("TSingleInstance : Only one instance of TSingleInstance is allowed per application.");
}
ThisSingleInstance = this;
}
//-------------------------------------------------------------

~ JD
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Development) 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.