Main Logo
Articles   Members Online:
-Article/Tip Search
-News Group Search over 800,000 Borland news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
Delphi: Check if a component has a property - and change it without typecasting
Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
Check if a component has a property - and change it without typecasting Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
10-Aug-04
Category
VCL-General
Language
Delphi All Versions
Views
594
User Rating
8.666666
# Votes
3
Replies
0
Publisher:
Philipp, Steve
Reference URL:
Steve Philipp
			1   
2   //Pass a component as TComponent generically,
3   //and change a property value
4   //- only if the component publishes that property -
5   //without typecasting.
6   
7   unit Unit1;
8   
9   interface
10  
11  uses
12    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
13    Dialogs,TypInfo, StdCtrls, ExtCtrls;
14  
15  type
16    TForm1 = class(TForm)
17      Edit1: TEdit;
18      Memo1: TMemo;
19      CheckBox1: TCheckBox;
20      RadioGroup1: TRadioGroup;
21      procedure FormCreate(Sender: TObject);
22    private
23      { Private declarations }
24    public
25      { Public declarations }
26    end;
27  
28  var
29    Form1: TForm1;
30  
31  implementation
32  
33  {$R *.dfm}
34  
35  uses
36    TypInfo;
37  
38  procedure ChangeProperty(C: TComponent; const Name: string; Val: string);
39  var
40    PropInfo: TypInfo.PPropInfo;
41  begin
42    PropInfo:= GetPropInfo(C.ClassInfo, Name);
43    if PropInfo <> nil then TypInfo.SetPropValue(C, Name, Val);
44  end;
45  
46  //This example will change the Text property
47  //of every component on the form
48  //if the component owns a PUBLISHED Text property;
49  //otherwise it will skip the component.
50  
51  procedure TForm1.FormCreate(Sender: TObject);
52  var
53    i: Integer;
54  begin
55    for i:= 0 to Pred(ComponentCount) do
56      ChangeProperty(Components[i], 'Text', 'Changed');
57  end;
58  //Check out TypInfo, it has many useful properties!
59  
60  end.


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC
Advertisement


Share this page
Advertisement
Copyright © 2024 by No Limit Solutions LLC