Author: Jonas Bilinkevicius
How to determine if an object has a particular property
Answer:
The first hasprop will return True if a property of name prop exists, eg.
hasprop(MyLabel,'Caption') will return true while hasprop(MyEdit,'Caption') will
return false. The second one will set property prop to string value s if it exists
and is a string type property.
1 hasprop(comp: TComponent; const prop: string): Boolean;
2 var
3 proplist: PPropList;
4 numprops, i: Integer;
5 begin
6 result := false;
7 getmem(proplist, getTypeData(comp.classinfo)^.propcount * Sizeof(Pointer));
8 try
9 NumProps := getproplist(comp.classInfo, tkProperties, proplist);
10 for i := 0 to pred(NumProps) do
11 begin
12 if comparetext(proplist[i]^.Name, prop) = 0 then
13 begin
14 result := true;
15 break;
16 end;
17 end;
18 finally
19 freemem(proplist, getTypeData(comp.classinfo)^.propcount * Sizeof(Pointer));
20 end;
21 end;
22
23 procedure setcomppropstring(comp: TComponent; const prop, s: string);
24 var
25 proplist: PPropList;
26 numprops, i: Integer;
27 begin
28 getmem(proplist, getTypeData(comp.classinfo)^.propcount * Sizeof(Pointer));
29 try
30 NumProps := getproplist(comp.classInfo, tkProperties, proplist);
31 for i := 0 to pred(NumProps) do
32 begin
33 if (comparetext(proplist[i]^.Name, prop) = 0) and
34 (comparetext(proplist[i]^.proptype^.name, 'string') = 0 then
35 begin
36 setStrProp(comp, proplist[i], s);
37 break;
38 end;
39 end;
40 finally
41 freemem(proplist, getTypeData(comp.classinfo)^.propcount * Sizeof(Pointer));
42 end;
43 end;
|