Solved

Reffering to a component created on run

Posted on 2006-06-27
11
184 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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 gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now