Solved

Reffering to a component created on run

Posted on 2006-06-27
11
186 Views
Last Modified: 2012-05-05
If I want to create a Label component  by "with TLabel.Create(self) do begin ..."  on program running, how can I  reffer to this component in code: eg. OnButtonClick - MyLabel.Caption :='Some text' ..., when this component practically does´nt exist?
0
Comment
Question by:JackKuti
11 Comments
 
LVL 14

Accepted Solution

by:
Pierre Cornelius earned 125 total points
ID: 16992744
Easiest is to save the reference to the label. i.e do "Mylabel:= TLable.Create" instead of with "TLabel.Create"

1.) under the private declarations of your form add a variable called MyLabel: TLabel;
2.) When you dynamically create it at run-time (in oncreate or wherever) do as such:

...
  MyLabel:= TLabel.Create(self); //
  Mylabel.parent:= self; //or whatever
  MyLabel.Caption:= 'whatever';
...

3.) refer to the said label anywhere in the form's scope

Regards
Pierre
0
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 16992772
If you give more info such as when you create it (e.g. when a button is clicked) and in which context (who will be parent, owner etc.) as well as the purpose (what are you trying to achieve), I might be able to give more insight/advice.
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16992943
var
  i : integer;
begin
  for i := 0 to ComponentCount - 1 do
  begin
    if Components[i] is TLabel then
    begin
      // TLabel(Components[i]).Caption
      // by checking it's caption, tag you can know that it is the one which you have created.
    end;
  end;
end;
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Expert Comment

by:new_x
ID: 16992950
As PierreC said you have to declare a variable of type TLabel then
hold the created instance in by using that variable.
So anywhere (where the scope rules allow) you can use that variable when referring the created label.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 16994013
Another way is:

  with TLabel.Create(self) do
  begin
    Name := 'Mylabel';
    // DO NOT FORGET THIS
    Parent := Form1;
    Caption := 'MyLabelCaption';
    Left := 10;
    Top := 25;
    Visible := True;
  end;

and then later...

 
  with (FindComponent('MyLabel') as TLabel) do
  begin
    Caption := 'Testing';
  end;


and to Free it:
  (FindComponent('MyLabel') as TLabel).Free;

0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 16994019
I want to say, however, that I DO NOT condone doing this at all. It takes no more meory
to create a variable named MyLabel.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 18264656
Actually, cwwkie, a split would be more appropriate as all three
gave good solutions.
0
 
LVL 14

Expert Comment

by:cwwkie
ID: 18269717
But....

http://www.experts-exchange.com/help.jsp#hi89
->    One expert's comments led (or would have led) to a solution. First person in with the best answer wins.

You gave a different solution, not neccessary better.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 18270312
ok
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

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…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 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