Solved

Set Property With string name

Posted on 2002-06-20
14
203 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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