Solved

Referencing a component through a variable

Posted on 2001-09-10
4
158 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 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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 censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

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