Board index » delphi » How can i assign a value prop. to a component of unknown type?

How can i assign a value prop. to a component of unknown type?

Hi, i'm developping a funtion to put all the DB Components in a form to
ReadOnly
and change the color, to know if is a DB Component i look if DataSource
property
exists (with GetPropInfo function). But how can assign a value to a property
of a
component that i don't know his type? Now i'm doing

Ex.-/
if Form1.Components[i] if TDBedit then
     TDBEdit(Form1.Components[i]).Color:=clred
else
if Form1.Components[i] if TDBComboBox then
     TDBComboBox(Form1.Components[i]).Color:=clred
else .......
.....

is there a generic function to do this without the cast of the component?

Thanks

 

Re:How can i assign a value prop. to a component of unknown type?


Quote
In article <3a8ab9b0_2@dnews>, Ladrillo wrote:
> Hi, i'm developping a funtion to put all the DB Components in a form to
> ReadOnly and change the color, to know if is a DB Component i look
> if DataSource property exists (with GetPropInfo function). But how can
> assign a value to a property of a component that i don't know his type?

The TypInfo unit you are already using has a bunch of methods to access
published properties by name. In D5 this is easier than in prior versions
since many of the basic routines available have overloaded versions that
are easier to use. The following will work in any Delphi version.

Uses typInfo;

Procedure SetColorProperty( forComponent: TComponent; value: TColor );
  Var
    PInfo: PPropInfo;
  Begin
    PInfo := GetPropInfo( forComponent.ClassInfo, 'color' );
    If Assigned( PInfo ) and (PInfo^.Proptype^.Kind = tkInteger) Then
      SetOrdProp( forComponent, PInfo, LongInt(value));
  End;

 SetColorProperty( Controls[i], clPuce );

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Note: I'm unable to visit the newsgroups every day at the moment,
so be patient if you don't get a reply immediately.

Other Threads