Solved

Referencing a component through a variable

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

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month4 days, 4 hours left to enroll

630 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