Solved

Reffering to a component created on run

Posted on 2006-06-27
11
185 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

19 Experts available now in Live!

Get 1:1 Help Now