DELPHI FAQ: Как установить значение строкового или целого поля если оно присутствует?


Как установить значение строкового или целого поля если оно присутствует?
Previous  Home  Next




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?
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
;




Tip by Xavier Pacheco



Взято из http://www.lmc-mediaagentur.de/dpool





DELPHI FAQ




EOMY TOP 100      Рейтинг@Mail.ru      Rambler's Top100