Dynamically access a property in a Delphi component
You have to use the Run-Time Type Information features of Delphi to do this:
This blog describes exactly what you are trying to do: Run-Time Type Information In Delphi - Can It Do Anything For You?
Basically you have to get the property information, using GetPropInfo
and then use SetOrdProp
to set the value.
uses TypInfo;
var
PropInfo: PPropInfo;
begin
PropInfo := GetPropInfo(Comp.ClassInfo, 'Left');
if Assigned(PropInfo) then
SetOrdProp(Comp, PropInfo, 100);
end;
This is not as concise as your pseudo-code, but it still does the job. Also it gets more complicated with other stuff, like array properties.
From one of my working units (in Delphi 7 though)
var
c : TComponent;
for i := 0 to pgcProjectEdits.Pages[iPage].ControlCount - 1 do
begin
c := pgcProjectEdits.Pages[iPage].Controls[i];
if c is TWinControl
then begin
if IsPublishedProp(c,'color')
then
SetPropValue(c,'color',clr);
if IsPublishedProp(c,'readonly')
then
SetPropValue(c,'readonly', bReadOnly );
...
end;
...
You have to include TypInfo
in the uses statement.
Don't know if this works under Delphi 5.