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
Solved

Reffering to a component created on run

Posted on 2006-06-27
11
187 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
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: 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

Suggested Solutions

Title # Comments Views Activity
Making delphi communicate with a c# service 16 111
When i run adoquery my application freezes 26 178
CheckListBox usage 3 71
firemonkey keyboard covers the controls 1 37
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…
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…

860 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