Solved

Reffering to a component created on run

Posted on 2006-06-27
11
188 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
[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
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

740 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