Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Referencing a component through a variable

Posted on 2001-09-10
4
Medium Priority
?
171 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:Mohammed Nasman
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 600 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

926 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