Solved

Referencing a component through a variable

Posted on 2001-09-10
4
161 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

738 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