Solved

Referencing a component through a variable

Posted on 2001-09-10
4
156 Views
Last Modified: 2010-04-06
How do i reference a component through a variable? For Example, if i have a variable containing 'panel1', how do i access panel1.caption? or the variable may contain panel4.....and then id want to access panel4.caption. If anybody could PLEASE answer this, i'd be very grateful.

Nick.
0
Comment
Question by:nameless_idiot
4 Comments
 
LVL 22

Expert Comment

by:mnasman
ID: 6472617
procedure TForm1.Button1Click(Sender: TObject);
var
  X : TPanel;
begin
  X := Panel1;
  X.Caption := 'Test Panel1';
  X := Panel2;
  X.Caption := 'Test Panel2';
end;
0
 
LVL 6

Accepted Solution

by:
Stuart_Johnson earned 150 total points
ID: 6472994
var
  CompName: String;

begin
  CompName := 'Panel1';
  TPanel(FindComponent(CompName)).Caption := 'Hi there!';
end;
0
 
LVL 2

Expert Comment

by:ivobauer
ID: 6541979
Hi xxx!

It's also possible to handle several objects of different classes with one variable. For this approach to work all these classes must descend from a common base class:

procedure HandleDifferentControls(Control: TControl);
begin
  // do different action depending of the control
     class type...
  if Control is TPanel then
    TPanel(Control).BevelInner := bsNone
  else if Control is TEdit then
    TEdit(Control).ReadOnly := True
  else
    ShowMessage('Control class not supported.');
end;

now you can call this procedure like

  HandleDifferentControls(Edit1);
  HandleDifferentControls(Panel1);
  HandleDifferentControls(Label1); // you get unsupported message


Hope this helps, Ivo.

If anything is not clear to you, don't hesitate to ask me.
0
 
LVL 17

Expert Comment

by:geobul
ID: 9296415
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

accept Stuart Johnson's comment as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thanks,

geobul
EE Cleanup Volunteer
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now