|
Access properties at runtime
The retreiving and changing the property value can be done by special access properties: AsFloat, AsMethod, AsInteger, AsChar, AsBoolean, AsObject, AsDateTime, AsDate, AsTime, AsString, AsVariant. Just use the appropriated property to get/set the value. Nothe that the AsString property can be used for any property type and the following ways will have the same effect.
var
P: TProperty;
...
P:=SomePropertyList.FindProperty('Color');
P.AsInteger:=clRed;
P.AsString:='clRed';
Each property has Properties property (TPropertyList type) that contains the subproperties (like properties of the Font property in TControl) and the FullName property the contains the name of the property and the names of all owner properties divided by points. The FindProperty method of TPropertyList class supports full names, so the code
procedure TForm1.FormClick(Sender: TObject);
var
P1,P2: TProperty;
begin
with TPropertyList.Create(nil) do
try
Instance:=Self;
Root:=Self;
P1:=FindProperty('Font');
P2:=P1.Properties.FindProperty('Color');
P2.AsInteger:=clRed;
finally
Free;
end;
end;
can be simplified to the code
procedure TForm1.FormClick(Sender: TObject);
var
P: TProperty;
begin
with TPropertyList.Create(nil) do
try
Instance:=Self;
Root:=Self;
P:=FindProperty('Font.Color');
P.AsInteger:=clRed;
finally
Free;
end;
end;
|