Link to home
Start Free TrialLog in
Avatar of MoonCalf
MoonCalf

asked on

TCustomPanel - Default Caption

Hi.

I've created a component that is a descendant of TCustomPanel.  The problem I currently have with it is the fact that it has a caption (which I've never understood for TPanels anyway!), and the caption is the component's name.

I want to remove that or set it to '' when created.  I've added code (Caption:='') into the create event of the component, after the inherited call, and all the other code too, but it makes no difference.

Can someone advise me?

Ta,

Moonie.
Avatar of LRHGuy
LRHGuy

I created a TCustomPanel myself, overriding nothing, and it came up blank....so I can only suggest you check for something else setting it.

type
  tcp=class(tCustomPanel)
  public
    constructor Create(aOwner:tComponent);
  end;


constructor tcp.Create(aOwner: tComponent);
begin
  inherited Create(aOwner);
  caption:=''; //force blank, in case it's not
end;
Captions for panels are quite useful, actually. Since you can click on a panel, you can use a panel as a simplified button. You could also use the caption for a panel that is e.g. used for drag&drop purposes as a drop area. Captions are quite useful for panels...

The Caption property is defined in the TControl component thus every control has a caption. It's just that in many cases the caption stays protected. But the Caption property and the Text property are actually pointing to the same string value that is stored within the Window associated to the control. It's actually the Window text.
If you inherit it from TCustomPanel then the Caption property is probably still protected and would not show in the Delphi designer. Only TPanel itself is publishing it. So you could create your own published property instead.

If you change the Text property, it will change the Caption property too, though...
Avatar of MoonCalf

ASKER

Hi.

When you created it, did you give it a name?  A simple create is fine for what you've suggested but I'm naming the component when it's created in order to identify it later.

It's the name that shows in the caption.  I didn't have this problem until I started naming the components.  I did consider using a different property, such as "identity", instead of the name, but that's not really a solution to the problem - more a way round it.  I'd rather know how to handle this problem for when I face it in different arenas.

Thanks,

MoonCalf.
Hi Alex - thanks for the comments, but I don't see how that helps.  I did try setting the Text property to '' also, but to no avail.

Thanks,

MoonCalf.
Incidentally, I've already made the caption property a published one in case it was due to it being protected.

MoonCalf.
Actually, after a little more investigation, I've noticed that both the caption and the text properties do absolutely nothing to my component.

AGGHHH!!!!

Help?

Moonie.
ASKER CERTIFIED SOLUTION
Avatar of LRHGuy
LRHGuy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
BTW, I do that in the create() method...
Cheers LRHGuy - I'll give it a go.
Nice - works perfectly.

There is one other wierdness about this component - it's not another question, but if anyone wants to offer anything it will be gratefully accepted!

It works fine now, for my purposes anyway.  I'm creating the components at runtime and assigning all the properties programmatically (each component relates to a file.)

Anyway, if I drop one on an empty form it does exactly what you'd expect.  The wierdness comes when I change the component name - name is not a published property in this case.  It's all fine until I leave the name field empty and press enter.  The component is visually still on the form, but not declared anywhere in the code.  (This obviously causes a runtime error when run, but the IDE's fine with it!)

Just thought it was strange so I'm sharing it : )

Thanks for all the help in the above problem.

MoonCalf.
The name property is used by the designer to add the component to your code. If you don't provide a name, it cannot generate a name in your code either. The component name has only one purpose: to provide a name to be used in your code. If you would create components dynamically, you can just skip providing a name.
It might or might not create a runtime error though. If you drop two TPanel components on your form and make the name of one of them empty then Delphi will just run fine. However, if you clear both names then your code doesn't know that you have a TPanel in your form. Even worse, it might not even realise what a TPanel is because it might not have a reference to the proper units. (Stripped away by the optimizer of Delphi.) Thus when it reads your form then it might not know what to do with a TPanel, thus crash.
However, if you use at least one TPanel in your code with a name, the optimizer will NOT strip away the class, thus Delphi is able to read it.

It is just fine to have components without a name on your form. It's just that you have to make sure the optimizer doesn't strip them away from your code so your code must have at least one reference to their classes. (And it gets a bit more complicated too from here onwards.)