1   
2   //Pass a component as TComponent generically,
3   //and change a property value
4   //- only if the component publishes that property -
5   //without typecasting.
6   
7   unit Unit1;
8   
9   interface
10  
11  uses
12    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
13    Dialogs,TypInfo, StdCtrls, ExtCtrls;
14  
15  type
16    TForm1 = class(TForm)
17      Edit1: TEdit;
18      Memo1: TMemo;
19      CheckBox1: TCheckBox;
20      RadioGroup1: TRadioGroup;
21      procedure FormCreate(Sender: TObject);
22    private
23      { Private declarations }
24    public
25      { Public declarations }
26    end;
27  
28  var
29    Form1: TForm1;
30  
31  implementation
32  
33  {$R *.dfm}
34  
35  uses
36    TypInfo;
37  
38  procedure ChangeProperty(C: TComponent; const Name: string; Val: string);
39  var
40    PropInfo: TypInfo.PPropInfo;
41  begin
42    PropInfo:= GetPropInfo(C.ClassInfo, Name);
43    if PropInfo <> nil then TypInfo.SetPropValue(C, Name, Val);
44  end;
45  
46  //This example will change the Text property
47  //of every component on the form
48  //if the component owns a PUBLISHED Text property;
49  //otherwise it will skip the component.
50  
51  procedure TForm1.FormCreate(Sender: TObject);
52  var
53    i: Integer;
54  begin
55    for i:= 0 to Pred(ComponentCount) do
56      ChangeProperty(Components[i], 'Text', 'Changed');
57  end;
58  //Check out TypInfo, it has many useful properties!
59  
60  end.
			
           |