>    { Look up the run-time info for the property named Color. }
>    PropInfo := GetPropInfo(Components[I].ClassInfo, 'Color');
>    if PropInfo <> nil then
>      { Set the Color property. }
>      SetOrdProp(Components[I], PropInfo, Color);
>  end;

That will work properly only if 'Color' is a property of type TColor
(although admittedly it's likely to be).  Here's a function we use:

procedure SetOrdinalProperty(Object: TObject; PropertyName: string;
PropType: TypeInfo; PropValue: LongInt);
var PropInfo: PPropInfo;
begin
  PropInfo := TypInfo.GetPropInfo(Object.ClassInfo, PropertyName);
  if (PropInfo <> nil) and (PropInfo^.PropType^ = PropType) then begin
    SetOrdProp(Object, PropInfo, PropValue);
  end;
end;

Then we can call it for any ordinal property.  The call to set the
'Color' property of a component to clWindow, only if it has such a
property and the property is of type TColor, would be as follows:

SetOrdinalProperty(Components[i], 'Color', System.TypeInfo(TColor),
clWindow);

Cheers,

Carl Reynolds                      Ph: +64-9-4154790
CJN Technologies Ltd.             Fax: +64-9-4154791
[EMAIL PROTECTED]                DDI: +64-9-4154795
PO Box 302-278, North Harbour, Auckland, New Zealand
12 Piermark Drive, North Harbour Estate, Auckland, NZ
Visit our website at http://www.cjntech.co.nz/

-----Original Message-----
From:   Kieron Lawson [SMTP:[EMAIL PROTECTED]]
Sent:   Wednesday, October 13, 1999 10:03 PM
To:     Multiple recipients of list delphi
Subject:        Re: [DUG]:  Changing control style


Wednesday, October 13, 1999, 9:43:10 PM, you wrote:

> I have a panel which has a number of controls (dbedits, dbcheckbox,
etc) and
> I want to change the background colour of each control when the Insert
> button is clicked. I've tried using the Controls property like so:

>   for x := 0 to Panel5.ControlCount - 1 do
>   begin
>     MyControl := Panel5.Controls[x];
>     MyControl.Color := clRed;
>   end;

>  but Color is a protected property of TControl. Is there any other way
of
> doing this without hard-coding each individual control. I also want to
> change the colour back when the new record is posted.

Hi Laurence,

If you have Secrets of Delphi 2 this is the first subject dealt with
in chapter 7.

If you don't, here's the code example:

procedure TForm1.ChangeColorsTo(Color: TColor);
var
  I: Integer;
  PropInfo: PPropInfo;
begin
  { Look up each component on the form. }
  for I := 0 to ComponentCount-1 do
  begin
    { Look up the run-time info for the property named Color. }
    PropInfo := GetPropInfo(Components[I].ClassInfo, 'Color');
    if PropInfo <> nil then
      { Set the Color property. }
      SetOrdProp(Components[I], PropInfo, Color);
  end;
  { Finally, change the color of the form itself. }
  PropInfo := GetPropInfo(ClassInfo, 'Color');
  SetOrdProp(Self, PropInfo, Color);
end;

It relies on the fact that Color is a published property and
therefore has run-time type information available.

As mentioned previously on this list, this is a book well worth owning,
although I understand that there is an updated Delphi 5 version in the
works.


Kieron.


------------------------------------------------------------------------
---
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

application/ms-tnef

Reply via email to