RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TypInfo.SetEnumProp Function

Sets the value of a component property that is an enumerated type.

Pascal
procedure SetEnumProp(Instance: TObject; const PropName: string; const Value: string); overload;
procedure SetEnumProp(Instance: TObject; PropInfo: PPropInfo; const Value: string); overload;
C++
SetEnumProp(TObject * Instance, const AnsiString PropName, const AnsiString Value);
SetEnumProp(TObject * Instance, PPropInfo PropInfo, const AnsiString Value);

SetEnumProp utilises Delphi's RTTI (Run Time Type Information) to set the value of a component's property where that property is an enumerated type.  

In one form of this function, the object Instance's property is defined by a PropInfo record. GetPropInfo and GetPropList can be used to obtain such a record.  

In the other form of this function, the name of the property, PropName, is explicitly given.  

In both forms of the function, the new property value is defined by Value.  

The intrinsic value of this function is to allow setting of component properties without the need to hard code the property name. This allows mass visual component processing at run time.  

Use SetPropValue to set a property value where the type is unknown. This would normally be used when using GetPropValue from another property.

Note: If the specified property does not exist, or is not a published property, an EPropertyError exception is thrown.
 

C++ Examples: 

 

/*
This example shows how to use GetEnumProp and SetEnumProp in order to
check for visual components properties and to change them.
This example uses two buttons, Button1 and Button2.
*/
void __fastcall TForm2::Button1Click(TObject *Sender)
{
    //getting the current color of the workspace
    String currentPropColor = GetEnumProp(this,"Color");
    int currentColorInt = StrToInt(currentPropColor);

    //getting the first button align enum and if different,
    //setting it to alLeft
    String currentAlignProp = GetEnumProp(Button1, "Align");
    if (currentAlignProp != "alLeft")
    {
         SetEnumProp(Button1, "Align", "alLeft");
     }

    //checking if the form background color was set.
    if(currentColorInt < 0)
    {
        currentColorInt = GetSysColor(COLOR_APPWORKSPACE);
    }

    //setting the form background color as the negative value
    //of the current background color
    SetEnumProp(this, "Color",
                IntToStr((int)(clWhite - currentColorInt)));
}

void __fastcall TForm2::Button2Click(TObject *Sender)
{
    int p[5] = {clYellow, clGreen, clRed, clBlue, clBlack};
    SetEnumProp(this, "Color", IntToStr(p[random(5)]));

    //getting the second button align enum and if different,
    //setting it to alRight
    String currentAlignProp = GetEnumProp(Button2, "Align");
    if (currentAlignProp != "alRight")
    {
         SetEnumProp(Button2, "Align", "alRight");
     }
}

 

Delphi Examples: 

{
This example shows how to use GetEnumProp and SetEnumProp in order to
check for visual components properties and to change them.
This example uses two buttons, Button1 and Button2.
}
procedure TForm3.Button1Click(Sender: TObject);
var
  currentPropColor : String;
  currentAlignProp : String;
  currentColorInt : Integer;
begin
  //getting the current color of the workspace
  currentPropColor := GetEnumProp(Self,'Color');
  currentColorInt := StrToInt(currentPropColor);

  //getting the first button align enum and if different,
  //setting it to alLeft
  currentAlignProp := GetEnumProp(Button1, 'Align');
  if GetEnumValue(TypeInfo(TAlign), currentAlignProp) <> Ord(alLeft) then
     SetEnumProp(Button1, 'Align', 'alLeft');

  //checking if the form background color was set.
    if currentColorInt < 0 then
    begin
        currentColorInt := GetSysColor(COLOR_APPWORKSPACE);
    end;

  //setting the form background color as the negative value
  // of the current background color
  currentColorInt := clWhite - currentColorInt;

  SetEnumProp(Self, 'Color', IntToStr(currentColorInt));
end;

procedure TForm3.Button2Click(Sender: TObject);
var
  p : array [0..4] of Integer;
  currentAlignProp : String;
begin
  p[0] := clYellow;
  p[1] := clRed;
  p[2] := clBlue;
  p[3] := clBlack;
  p[4] := clGreen;

    SetEnumProp(Self, 'Color', IntToStr(p[random(5)]));

  //getting the second button align enum and if different,
  //setting it to alRight
  currentAlignProp := GetEnumProp(Button2, 'Align');
  if GetEnumValue(TypeInfo(TAlign), currentAlignProp) <> Ord(alRight) then
     SetEnumProp(Button2, 'Align', 'alRight');

end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!