VCL Delphi

//Pass a component as TComponent generically,
//and change a property value
//- only if the component publishes that property -
//without typecasting.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,TypInfo, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Memo1: TMemo;
CheckBox1: TCheckBox;
RadioGroup1: TRadioGroup;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
TypInfo;
procedure ChangeProperty(C: TComponent; const Name: string; Val: string);
var
PropInfo: TypInfo.PPropInfo;
begin
PropInfo:= GetPropInfo(C.ClassInfo, Name);
if PropInfo <> nil then TypInfo.SetPropValue(C, Name, Val);
end;
//This example will change the Text property
//of every component on the form
//if the component owns a PUBLISHED Text property;
//otherwise it will skip the component.
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
begin
for i:= 0 to Pred(ComponentCount) do
ChangeProperty(Components[i], 'Text', 'Changed');
end;
//Check out TypInfo, it has many useful properties!
end.