Solved

Set Property With string name

Posted on 2002-06-20
14
205 Views
Last Modified: 2013-11-23
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
Comment
Question by:S_Warrior
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 3
  • 2
  • +1
14 Comments
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 7095508
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
 
LVL 17

Expert Comment

by:geobul
ID: 7095615
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
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 7095693
nice :)
0
Independent Software Vendors: 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!

 

Author Comment

by:S_Warrior
ID: 7096206
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
 
LVL 11

Accepted Solution

by:
shaneholmes earned 100 total points
ID: 7096233
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
 

Author Comment

by:S_Warrior
ID: 7101393
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
 

Author Comment

by:S_Warrior
ID: 7101394
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
 

Author Comment

by:S_Warrior
ID: 7101396
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
 
LVL 17

Expert Comment

by:geobul
ID: 7101431
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
 

Author Comment

by:S_Warrior
ID: 7101492
geobul is it have to come from a FORM, or I can do the same thing from a CustomPanel?
0
 

Author Comment

by:S_Warrior
ID: 7101504
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
 

Author Comment

by:S_Warrior
ID: 7101506
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
 

Author Comment

by:S_Warrior
ID: 7101507
I dont know to who give the points so I give them to the first one how posted the answer
0
 
LVL 17

Expert Comment

by:geobul
ID: 7101531
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

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Performance of SQL statement 37 145
how to change, disabled button color FMX ? 1 63
Firemonkey allowing RTL on android 6 75
Can Live bindings change TGrid Cell Colour ? 1 61
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question