Board index » delphi » Passing controls as parameters

Passing controls as parameters

I have a procedure to change the color and font color of controls I
send to it. The problem is that the color properties seem to be
protected, so I have to do something like this-

procedure SetColors(Names: array of TWinControl; aColor, FontColor:
TColor);
Var
  x: integer;
begin
  for x := 0 to High(Names) do
  begin
    if Names[x] is TRxSpinEdit then
    begin
      with Names[x] as TRxSpinEdit do
      begin
        Color := aColor;
        Font.Color := FontColor;
      end;
    end;
  end;
end;

How can I do this without having to know what the class names are for
each item?

I've tried several things and I can't figure out how to do it. I
always get "undeclared indentifier" errors.

 

Re:Passing controls as parameters


The Color property is protected. You can gain access to it my declaring a
'cracker' class in your unit, i.e:

type
  TMyControl = class(TControl);

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
   if Sender is TControl
     then TMyControl(Sender).Color := clRed;
end;

Note that color change is handled by sending the CM_COLORCHANGED message to
the control, which then determines the correct response to the request.

Regards,
Peter Kobor
Mill Valley, CA

Re:Passing controls as parameters


In article <avd8bs80asge5ppobfn8oblc9cqb8sj...@4ax.com>, Ken P. <a...@123.net>
writes:

Quote
>I have a procedure to change the color and font color of controls I
>send to it. The problem is that the color properties seem to be
>protected, so I have to do something like this-

<snip>
>How can I do this without having to know what the class names are for
>each item?

>I've tried several things and I can't figure out how to do it. I
>always get "undeclared indentifier" errors.

Color and Font (and hence Font.Color) are indeed protected properties of a
TWinControl. The protected property (or method) is in the code but the compiler
does not let you access it. But the compiler allows access to a protected item
of a descendant class in the same unit as the descendant is declared in.

So declare a descendant of TWinControl. In the interface "type" clause enter :-

TColorWinControl = class(TWinControl); // I always use the reason I've declared
it in its name

Then use it as a typecast :-

procedure SetColors(Names: array of TWinControl; aColor, FontColor:
TColor);
Var
  x: integer;
begin
  for x := 0 to High(Names) do
    if Names[x] is TWinControl then
      with TColorWinControl(Names[x]) do begin
        Color := aColor;
        Font.Color := FontColor;
      {end;  with TColorWinControl(Names[x])}
    {end; if Names[x] is TWinControl}
  {end; for x := 0 to High(Names}
end;

Alan Lloyd
alangll...@aol.com

Other Threads