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 

Component needs to confirm parent before showing

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.components.writing
View previous topic :: View next topic  
Author Message
Bill Bateman
Guest





PostPosted: Thu Apr 07, 2005 7:18 am    Post subject: Component needs to confirm parent before showing Reply with quote



Hello all,

I am attempting to create some CustomControls that only drop on a
TPanel. However, in order to do this, I need to check the
Parent.Classtype for a match to TPanel. I cannot get access to Parent
in the constructor and the loaded procedure doesn't get fired. I
cannot use AOwner in the constructor because it always references
Tform.

Has anyone done this before that can help?

Bill

Back to top
Bruce Roberts
Guest





PostPosted: Thu Apr 07, 2005 3:32 pm    Post subject: Re: Component needs to confirm parent before showing Reply with quote




"Bill Bateman" <bbateman (AT) gmail (DOT) com> wrote


Quote:
I am attempting to create some CustomControls that only drop on a
TPanel. However, in order to do this, I need to check the
Parent.Classtype for a match to TPanel. I cannot get access to Parent
in the constructor and the loaded procedure doesn't get fired. I
cannot use AOwner in the constructor because it always references
Tform.

Parent isn't known until after component construction. You'll need to
override the SetParent method to prevent the assignement of anything but a
tCustomPanel, or whatever. IIRC something like the following should do it

procedure tSomeControl.SetParent (aParent : tWinControl);

begin
if not (aParent is tCustomPanel)
then raise exception.Create ('Drop me only on a panel');
inherited;
end;



Back to top
dhamdon@gmail.com
Guest





PostPosted: Thu Apr 07, 2005 4:19 pm    Post subject: Re: Component needs to confirm parent before showing Reply with quote



I have attempted this solution. At first it appears to work but then
you quickly find out that, even though the exception was thrown, the
Object is still written to the .DFM file. Which, I guess, means that
the streaming mechanism still writes the object definition even if you
throw an exception in the SetParent before or after the Inherited
call.

This seems to be so difficult to implement cleaning. Does this mean we
have the wrong solution to the problem? Did Borland intend for us to
do this in a different way? Like the TToolButton & TToolbar for
example. There you have to right-click to add a TToolbar. You cannot
drag one off the Component Palette. TToolbar isn't there.

Back to top
Bill Bateman
Guest





PostPosted: Thu Apr 07, 2005 4:23 pm    Post subject: Re: Component needs to confirm parent before showing Reply with quote

I saw how the toolbar was implemented, however, that control only
allows for the layout IT designates, not custom layouts that would be
able to mimic, say, Microsoft interfaces. This way, I can drop combos,
buttons, etc on the panel/toolbar, in any layout, but they need only to
work on that panel I created because the paint needs to know
information from that specific parent.

Bill

Back to top
Bruce Roberts
Guest





PostPosted: Thu Apr 07, 2005 8:18 pm    Post subject: Re: Component needs to confirm parent before showing Reply with quote


<dhamdon (AT) gmail (DOT) com> wrote

Quote:
I have attempted this solution. At first it appears to work but then
you quickly find out that, even though the exception was thrown, the
Object is still written to the .DFM file. Which, I guess, means that
the streaming mechanism still writes the object definition even if you
throw an exception in the SetParent before or after the Inherited
call.

At the time Parent is set, Owner already has a value - typically the form.
Owner is responsible for streaming, so I'm not sure that there is a clean
way to get rid of the thing.

You might consider, instead, simply having your controls not function if
Parent isn't a valid control.




Back to top
Bill Bateman
Guest





PostPosted: Sat Apr 09, 2005 5:21 am    Post subject: Re: Component needs to confirm parent before showing Reply with quote

Still not the cleanest or most complete solution. I am quite
surprised that this is not possible. I am posting this in a yahoo
group to see if I can get any other answers. I will post any 'good'
responses here if i get them.

Bill

Back to top
Bruce Roberts
Guest





PostPosted: Mon Apr 11, 2005 1:57 pm    Post subject: Re: Component needs to confirm parent before showing Reply with quote


"Bill Bateman" <bbateman (AT) gmail (DOT) com> wrote

Quote:
Still not the cleanest or most complete solution. I am quite
surprised that this is not possible. I am posting this in a yahoo
group to see if I can get any other answers. I will post any 'good'
responses here if i get them.

I don't see why its so surprising. If you want a panel to only host a few
types of controls, rather than any hostable control, the panel should be the
one making the decision, not the sub-control.

You might want to take a look at tPageControl to see how it manages the
feat.



Back to top
Bill Bateman
Guest





PostPosted: Tue Apr 12, 2005 3:19 am    Post subject: Re: Component needs to confirm parent before showing Reply with quote

OK, here is the working code thanks to the Calgary Delphi Users Group:

What is needed is handling both cases; designtime and runtime.
Here is a small example:

type
TTestLabel = class(TLabel)
private
procedure CheckParent(AParent: TComponent);
protected
procedure ValidateContainer(AComponent: TComponent); override;
procedure SetParent(AParent: TWinControl); override;
end;

procedure TTestLabel.CheckParent(AParent: TComponent); begin
if Assigned(AParent) and not (AParent is TCustomPanel) then
raise Exception.Create('TTestLabel requires a TCustomPanel
parent'); end;

procedure TTestLabel.SetParent(AParent: TWinControl); begin
if not (csDesigning in ComponentState) then
CheckParent(AParent);
inherited SetParent(AParent);
end;

procedure TTestLabel.ValidateContainer(AComponent: TComponent); begin
inherited ValidateContainer(AComponent);
if csDesigning in ComponentState then
CheckParent(AComponent);
end;

Bill

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.components.writing 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.