Board index » delphi » Why won't this work?

Why won't this work?

I want to change the font name of all components on a form at run time.
They do not use parentfont = true;

Why do I get an "[Error] xxxx.pas(661): Undeclared identifier: 'font'"
with:
//defaultname is a string with the desired font name;

for j:=0 to FM.componentcount-1 do
  if (FM.Components[j] is tcontrol) then
    tcontrol(FM.Components[j]).font.name := defaultfontname;

I have even tried having a tcomponent variable, tc but get the same
error with

tc := tcontrol(FM.Components[j]); // this line works fine.

tc.font.name := defaultfontname; // this one doesn't.

I don't understand as I thought font is a property of tcontrol.

..Jim

 

Re:Why won't this work?


The problem is that TControl.Font is a protected property. This means that
it's there, but you can't actually use it. It's only there so that
descendant classes can make it public. (This is typical of many properties
of the VCL base classes).
But there is a very sneaky way to get at these protected properties. All you
have to do is declare your own component class as follows:

type
  TMyControl= class(TControl)
  public
    property Font;
  end;

Now just use TMyControl instead of TControl in your typecasts, and it should
all be clear sailing.

Best of luck,

Tristan.

Quote
Jim Andrews wrote in message <3B37BE79.C6A2A...@azdogs.com>...
>I want to change the font name of all components on a form at run time.
>They do not use parentfont = true;

>Why do I get an "[Error] xxxx.pas(661): Undeclared identifier: 'font'"
>with:
>//defaultname is a string with the desired font name;

>for j:=0 to FM.componentcount-1 do
>  if (FM.Components[j] is tcontrol) then
>    tcontrol(FM.Components[j]).font.name := defaultfontname;

>I have even tried having a tcomponent variable, tc but get the same
>error with

>tc := tcontrol(FM.Components[j]); // this line works fine.

>tc.font.name := defaultfontname; // this one doesn't.

>I don't understand as I thought font is a property of tcontrol.

>..Jim

Re:Why won't this work?


Quote
"Tristan" <moonsh...@apex.net.au> wrote in message
> type
>   TMyControl= class(TControl)
>   public
>     property Font;
>   end;

One can save a little typing by using the following declaration instead.

Type
tMyControl = class (tControl);

Since Font is a protected property and since a unit that declares a
descendant can see all protected identifiers one doesn't actually need to
change the visibility of Font.

Re:Why won't this work?


Touche
: )
Quote
Bruce Roberts wrote in message ...

>"Tristan" <moonsh...@apex.net.au> wrote in message

>> type
>>   TMyControl= class(TControl)
>>   public
>>     property Font;
>>   end;

>One can save a little typing by using the following declaration instead.

>Type
>tMyControl = class (tControl);

>Since Font is a protected property and since a unit that declares a
>descendant can see all protected identifiers one doesn't actually need to
>change the visibility of Font.

Re:Why won't this work?


Thanks, worked fine.
Quote
Tristan wrote:
> Touche
> : )

> Bruce Roberts wrote in message ...

> >"Tristan" <moonsh...@apex.net.au> wrote in message

> >> type
> >>   TMyControl= class(TControl)
> >>   public
> >>     property Font;
> >>   end;

> >One can save a little typing by using the following declaration instead.

> >Type
> >tMyControl = class (tControl);

> >Since Font is a protected property and since a unit that declares a
> >descendant can see all protected identifiers one doesn't actually need to
> >change the visibility of Font.

Other Threads