How to determine if an object has a particular property (Views: 708)
Problem/Question/Abstract: 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. function hasprop(comp: TComponent; const prop: string): Boolean; var proplist: PPropList; numprops, i: Integer; begin result := false; getmem(proplist, getTypeData(comp.classinfo)^.propcount * Sizeof(Pointer)); try NumProps := getproplist(comp.classInfo, tkProperties, proplist); for i := 0 to pred(NumProps) do begin if comparetext(proplist[i]^.Name, prop) = 0 then begin result := true; break; end; end; finally freemem(proplist, getTypeData(comp.classinfo)^.propcount * Sizeof(Pointer)); end; end; procedure setcomppropstring(comp: TComponent; const prop, s: string); var proplist: PPropList; numprops, i: Integer; begin getmem(proplist, getTypeData(comp.classinfo)^.propcount * Sizeof(Pointer)); try NumProps := getproplist(comp.classInfo, tkProperties, proplist); for i := 0 to pred(NumProps) do begin if (comparetext(proplist[i]^.Name, prop) = 0) and (comparetext(proplist[i]^.proptype^.name, 'string') = 0 then begin setStrProp(comp, proplist[i], s); break; end; end; finally freemem(proplist, getTypeData(comp.classinfo)^.propcount * Sizeof(Pointer)); end; end; |