How to set a string or integer property for a component if it exists (Views: 30)
Problem/Question/Abstract: 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: procedure SetStringPropertyIfExists(AComp: TComponent; APropName: string; AValue: string); var PropInfo: PPropInfo; TK: TTypeKind; begin PropInfo := GetPropInfo(AComp.ClassInfo, APropName); if PropInfo <> nil then begin TK := PropInfo^.PropType^.Kind; if (TK = tkString) or (TK = tkLString) or (TK = tkWString) then SetStrProp(AComp, PropInfo, AValue); end; end; procedure SetIntegerPropertyIfExists(AComp: TComponent; APropName: string; AValue: Integer); var PropInfo: PPropInfo; begin PropInfo := GetPropInfo(AComp.ClassInfo, APropName); if PropInfo <> nil then begin if PropInfo^.PropType^.Kind = tkInteger then SetOrdProp(AComp, PropInfo, AValue); end; end; |