Board index » cppbuilder » How to set property value given property name

How to set property value given property name

I have some code that wants to work with a variety of standard controls,
and one thing it needs to do is set the Color or ParentColor properties
if published.

I'm not having a lot of luck with the RTTI documentation or Google.
Does anyone have any sample code [in Delphi or BCB] that shows how to
query a component for a property by name, and then set its value?

--
AlisdairM
Team Thai Kingdom

 

Re:How to set property value given property name


Hi,

Hem... Maybe not the solution you expected. But I do that simply by the use
of try/catch blocks.
So you assume that all yours control have a given property and you catch the
generated exception.

Sorry if that doesn't help you.

Regards,

Re:How to set property value given property name


Quote
Alisdair Meredith <alisdair.mered...@uk.renaultf1.com> wrote:
>I have some code that wants to work with a variety of standard
>controls, and one thing it needs to do is set the Color or
>ParentColor properties if published.

>I'm not having a lot of luck with the RTTI documentation or
>Google. Does anyone have any sample code [in Delphi or BCB]
>that shows how to query a component for a property by name,
>and then set its value?

Here you go:

  TComponent* Component = Form1; // (for example)
  TColor color = clBlue;
  PPropInfo PropInfo = GetPropInfo((PTypeInfo)Component->ClassInfo(), "Color");
  if(PropInfo)
    SetOrdProp(static_cast<TObject *>(Component), PropInfo, (int)color);

Find more information at
 http://www.blong.com/Conferences/BorConUK98/C++RTTI/CB140.htm

Re:How to set property value given property name


Quote
Thorsten Kettner wrote:
> Here you go:

>   TComponent* Component = Form1; // (for example)
>   TColor color = clBlue;
>   PPropInfo PropInfo = GetPropInfo((PTypeInfo)Component->ClassInfo(), "Color");
>   if(PropInfo)
>     SetOrdProp(static_cast<TObject *>(Component), PropInfo, (int)color);

Thanks for the code, exactly what I was looking for <g>

Quote
> Find more information at
>  http://www.blong.com/Conferences/BorConUK98/C++RTTI/CB140.htm

Thanks for the link too <g>  Further reading is always useful!

In the meantime, I have fallen back on a dimly remembered, older hack
that SEEMS to do the job.  Curious if anyone can see any problems with
it  [It seems simpler for now, if cheating]

class TPublishColor : public TControl // I know my class is derived
{
public:
  __property Color;

Quote
};

void MyFun( TControl *pControl )
{
  assert( pControl );
  static_cast<TPublishColor>(pControl)->Color = clBlue;

Quote
}

--
AlisdairM
Team Thai Kingdom

Re:How to set property value given property name


Quote
kikof wrote:
> Hem... Maybe not the solution you expected. But I do that simply by the use
> of try/catch blocks.
> So you assume that all yours control have a given property and you catch the
> generated exception.

THanks, but I don't understand how you will do this.  I don't know how
to query the property by name at run-time [hence my question] and my
code will not compile if I try hardcoding the colour property.

--
AlisdairM
Team Thai Kingdom

Re:How to set property value given property name


Quote
> THanks, but I don't understand how you will do this.  I don't know how
> to query the property by name at run-time [hence my question] and my
> code will not compile if I try hardcoding the colour property.

Ok, just for fun... I use a similar code to resize all my components in a
window
depending on the screen resolution (so I used Height and Width properties).
In your case, it's a little more complex as the Color property is protected.

The code could be like :

In header :

class THackControl : public TControl // thank Gambit
{
    private:
     void HackSetColor(TColor tc) {TControl::Color = tc;};
    public:
      __property TColor Color = {write=HackSetColor};

Quote
}; // grant access to protected property Color of TControl

In cpp file :

THackControl* phtc;

for (int i=0; i<ComponentCount; i++) { // The life in green

    try {
        phtc = (THackControl*)Components[i];
        phtc->Color = clGreen;
    } catch(...) {}

Quote
}

Voila !

Other Threads