Author: Tomas Rutkauskas
I am building a routine that checks our forms for validity before deploying them. I
would like to use some kind of structure that tests if a component type has access
to a certain property, something like: " if (self.Controls[b] has Tag) then ...".
Can anyone offer suggestions?
Answer:
Here's an example of setting a string property for a component if it exists and
another for an integer property:
1 procedure SetStringPropertyIfExists(AComp: TComponent; APropName: string;
2 AValue: string);
3 var4 PropInfo: PPropInfo;
5 TK: TTypeKind;
6 begin7 PropInfo := GetPropInfo(AComp.ClassInfo, APropName);
8 if PropInfo <> nilthen9 begin10 TK := PropInfo^.PropType^.Kind;
11 if (TK = tkString) or (TK = tkLString) or (TK = tkWString) then12 SetStrProp(AComp, PropInfo, AValue);
13 end;
14 end;
15 16 procedure SetIntegerPropertyIfExists(AComp: TComponent; APropName: string;
17 AValue: Integer);
18 var19 PropInfo: PPropInfo;
20 begin21 PropInfo := GetPropInfo(AComp.ClassInfo, APropName);
22 if PropInfo <> nilthen23 begin24 if PropInfo^.PropType^.Kind = tkInteger then25 SetOrdProp(AComp, PropInfo, AValue);
26 end;
27 end;