• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

Set Property With string name

Hello,

How can I set Property of a componet at Run-Time,
I have the Property as a string value?

I mean if I run Inputquery and the user enters Name of a property I can set a value to this property
0
S_Warrior
Asked:
S_Warrior
  • 8
  • 3
  • 2
  • +1
1 Solution
 
Lee_NoverCommented:
I don't think so
you could find the SetProperty methods name but not all properties have a Get/Set method

like :

procedure TForm1.Button1Click(Sender: TObject);
type
  TSetCaption = procedure(Value: string);
var newcap: string;
    SetCapM: TSetCaption;
begin
     if not InputQuery('Enter the new caption', 'new caption:', newcap) then exit;
     @SetCapM:=MethodAddress('SetCaption');
     if @SetCapM = nil then exit;
     SetCapM(newcap);
end;


for this to work the SetMethod must be in public scope
0
 
geobulCommented:
Hi,

uses
 TypInfo;

function SetProperty(fForm: TForm; sControl: string; sProperty: string; value: variant): integer;
var
  i: integer;
  PRecProp: PPropInfo;
begin
 result := 0;
 try
  for i := 0 to fForm.ComponentCount - 1 do
    if (fForm.Components[i] is TControl) then
      if (fForm.Components[i]).Name = sControl then begin
        PRecProp := GetPropInfo(fForm.Components[i].ClassInfo, sProperty);
        if Assigned(PRecProp) then
          case VarType(value) of
            varInteger, varBoolean: SetOrdProp(fForm.Components[i], PRecProp, value);
            varString: SetOrdProp(fForm.Components[i], PRecProp, integer(string(value)));
          end;
        break;
      end;
 except
   result := 1;
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetProperty(Form1,'Label1','color', clRed);
  SetProperty(Form1,'Label1','caption', 'Hello world!!!');
  SetProperty(Form1,'Edit1','enabled', false);
end;

Regards, Geo
0
 
Lee_NoverCommented:
nice :)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
S_WarriorAuthor Commented:
Very nice,

only small question how can I get the property?

I tried GetOrdProp() but I am not sure its working

and it return only integer
0
 
shaneholmesCommented:
Getting and Setting an object's property at runtime.

function GetProperty(Sender:TComponent; sPropName:String):Variant
var    
 PropInfo : PPropinfo;
begin    
 PropInfo := GetPropInfo(Sender.ClassInfo,sPropName);      
 If (PropInfo <> Nil) then
 begin        
  Case propinfo^.PropType^.Kind Of  tkEnumeration,tkInteger:
 begin                
  result :=  GetOrdProp(Sender,PropInfo)
 end;
tkString,tkLString,tkWString:
 begin                
  result := GetStrProp(Sender,PropInfo)
 end;
end;
end
else
result := Null;
end;


procedure SetProperty(Sender:TComponent ;sPropName:String                       ;vValue:Variant)
var    
 PropInfo : PPropinfo;
begin
 PropInfo := GetPropInfo(Sender.ClassInfo,                              sPropName);
 If (PropInfo <> Nil) then
 begin
  Case propinfo^.PropType^.Kind Of            tkEnumeration,tkInteger:
 begin
  SetOrdProp(Sender,PropInfo,vValue);
 end;            tkString,tkLString,tkWString:
 begin
  SetStrProp(Sender,PropInfo,vValue);
 end;
 end;
end;
end;

Examples:

SetProperty(DBEdit1,'Ctl3D',False);    
DBEdit2.Ctrl3D :=  GetProperty(DBEdit1.'Ctl3D')
0
 
S_WarriorAuthor Commented:
I have a problaem, the code is working only on properies of the TComponent, Like Left, top, Visible etc...

but whan I have a custom build component that is add some more properies that dont come with delphi I can't see them with the code.

Please give a code that will extract all the properties there is.
0
 
S_WarriorAuthor Commented:
I have a problaem, the code is working only on properies of the TComponent, Like Left, top, Visible etc...

but whan I have a custom build component that is add some more properies that dont come with delphi I can't see them with the code.

Please give a code that will extract all the properties there is.
0
 
S_WarriorAuthor Commented:
I have a problaem, the code is working only on properies of the TComponent, Like Left, top, Visible etc...

but whan I have a custom build component that is add some more properies that dont come with delphi I can't see them with the code.

Please give a code that will extract all the properties there is.
0
 
geobulCommented:
The following works fine I think. A custom component 'TMyEdit' derived from TEdit has a new property 'Str'. Button1 reads the property value and Button2 sets that value.

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TMyEdit = class(TEdit)
  private
    FStr: string;
  published
    property Str : string read FStr write FStr;
  end;

var
  Form1: TForm1;
  MyEdit: TMyEdit;

implementation

{$R *.DFM}

uses TypInfo;

function GetProperty(fForm: TForm; sControl: string; sProperty: string) : variant;
var
  i: integer;
  PRecProp: PPropInfo;
begin
   for i := 0 to fForm.ComponentCount - 1 do
    if (fForm.Components[i] is TControl) then
      if (fForm.Components[i]).Name = sControl then begin
        PRecProp := GetPropInfo(fForm.Components[i].ClassInfo, sProperty);
        if Assigned(PRecProp) then
          case PRecProp^.PropType^.Kind of
            tkEnumeration,tkInteger:
              result := GetOrdProp((fForm.Components[i] as TComponent), PRecProp);
            tkFloat:
              result := GetFloatProp((fForm.Components[i] as TComponent), PRecProp);
            tkString,tkLString,tkWString:
              result := GetStrProp((fForm.Components[i] as TComponent), PRecProp);
            tkVariant:
              result := GetVariantProp((fForm.Components[i] as TComponent), PRecProp);
          end;
      end;
end;

function SetProperty(fForm: TForm; sControl: string; sProperty: string; value: variant): integer;
var
 i: integer;
 PRecProp: PPropInfo;
begin
result := 0;
try
 for i := 0 to fForm.ComponentCount - 1 do
   if (fForm.Components[i] is TControl) then
     if (fForm.Components[i]).Name = sControl then begin
       PRecProp := GetPropInfo(fForm.Components[i].ClassInfo, sProperty);
       if Assigned(PRecProp) then
         case VarType(value) of
           varInteger, varSmallint, varBoolean: SetOrdProp(fForm.Components[i], PRecProp, value);
           varSingle, varDouble, varCurrency, varDate: SetFloatProp(fForm.Components[i], PRecProp, value);
           varString: SetStrProp(fForm.Components[i], PRecProp, string(value));
         end;
       break;
     end;
except
  result := 1;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  MyEdit := TMyEdit.Create(Form1);
  with MyEdit do begin
    Parent := Form1;
    Name := 'MyEdit1';
    Left := 100;
    Top := 50;
    Str := 'geobul';
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  s: string;
begin
  s := GetProperty(Form1, 'MyEdit1', 'Str');
  ShowMessage(s);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if SetProperty(Form1, 'MyEdit1', 'Str', 'S_Warrior') <> 0 then ShowMessage('Error setting the property');
end;
0
 
S_WarriorAuthor Commented:
geobul is it have to come from a FORM, or I can do the same thing from a CustomPanel?
0
 
S_WarriorAuthor Commented:
I dont why but this dont work on my Components.

I will publish the type's maby it will help
TMyEdit = class(TLabelControl
\/
TLabelControl = class(TCustomControl)
\/
TCustomControl = class(TWinControl)
ETC.....

I have properties in TMyEdit AND in TLabelControl and I want to show them all on screen.

but nothing happens if I request for property that I did like strCompName

but whan I request property like LEFT its working fine.

WHY?!?!?!

Please help
0
 
S_WarriorAuthor Commented:
SHITTTTTT I found the probleam SORRY GUYS I WORKING TO MUCH :)

the properties was in the public and not in the Published area

SORRY
0
 
S_WarriorAuthor Commented:
I dont know to who give the points so I give them to the first one how posted the answer
0
 
geobulCommented:
Well,
-you asked 'How can I set Property of a componet at Run-Time...' and I asked first.
-there was a published property 'Str' in my TMyEdit example.

and finally ?
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 8
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now