Author: Jonas Bilinkevicius
I use setpropvalue() function to set property values. It works fine if I set
properties that are on first level (i.e.. button1.caption, button1.name,..), but it
fails if I want to set properties like button1.font.name or radiogroup.items.text.
Answer:
1 function GetProperty(AControl: TPersistent; AProperty: string): PPropInfo;
2 var3 i: Integer;
4 props: PPropList;
5 typeData: PTypeData;
6 begin7 Result := nil;
8 if (AControl = nil) or (AControl.ClassInfo = nil) then9 Exit;
10 typeData := GetTypeData(AControl.ClassInfo);
11 if (typeData = nil) or (typeData^.PropCount = 0) then12 Exit;
13 GetMem(props, typeData^.PropCount * SizeOf(Pointer));
14 try15 GetPropInfos(AControl.ClassInfo, props);
16 for i := 0 to typeData^.PropCount - 1 do17 begin18 with Props^[i]^ do19 if (Name = AProperty) then20 result := Props^[i];
21 end;
22 finally23 FreeMem(props);
24 end;
25 end;
26 27 procedure TForm1.Button1Click(Sender: TObject);
28 var29 propInfo: PPropInfo;
30 begin31 PropInfo := GetProperty(Button1.Font, 'Name');
32 if PropInfo <> nilthen33 SetStrProp(Button1.Font, PropInfo, 'Arial');
34 end;