Scalabium Software

SMExport advertising
Knowledge for your independence'.
Home Delphi and C++Builder tips

#40: How can I read and set the some property by name?

Sometimes you need to change a some property for each components. Of course, if these components have a common ancestor, then you can write:

var i: Integer;
  for i := 0 to ComponentCount-1 do
    if Components[i] is TyourAncestor then
       TyourAncestor(Components[i]).yourProperty := yourValue;

For example,

var i: Integer;
  for i := 0 to ComponentCount-1 do
    if Components[i] is TSpeedButton then
       TSpeedButton(Components[i]).Flat := True;

But frequently you need change a property but the components haven't a common ancestor. In an example above you can have a TSpeedButton's components and TDBNavigator component on same panel. These components have taken place from different ancesors and you must change a property as different. You can make following:

var i: Integer;
    PropInfo: PPropInfo;
  for i := 0 to ComponentCount-1 do
    PropInfo := GetPropInfo(Components[i].ClassInfo, yourPropertyName);
    {if such property exists}
    if Assigned(PropInfo) then 
      SetOrdProp(Components[i], PropInfo, LongInt(yourPropertyValue));

Of course, if you have not ordinary property (like Flat), you need call a different procedure. The following procedures were declared in typinfo.pas:

procedure SetOrdProp(Instance: TObject; PropInfo: PPropInfo;
  Value: Longint);

procedure SetStrProp(Instance: TObject; PropInfo: PPropInfo;
  const Value: string);

procedure SetFloatProp(Instance: TObject; PropInfo: PPropInfo;
  Value: Extended);

procedure SetVariantProp(Instance: TObject; PropInfo: PPropInfo;
  const Value: Variant);

procedure SetMethodProp(Instance: TObject; PropInfo: PPropInfo;
  const Value: TMethod);

Published: November 10, 1999

See also
DBExport tools
Paradox to Text converter
SMImport suite
dBase Viewer
Excel Web-stream
SMMsg suite
ABA Picture Convert
SMExport suite

Contact to webmaster


Borland Software Code Gear Scalabium Delphi tips

Copyright© 1998-2017, Scalabium Software. All rights reserved.

SMExport advertising