Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

RunTime Labels

Posted on 2001-07-17
6
Medium Priority
?
415 Views
Last Modified: 2010-04-06
I created an array of Labels at runtime.

LNames: TList;

procedure...
var L: TLabel; I: Integer;
begin
 for I := 0 to 9 do
 begin
 L := TLabel.Create(Form1);
 L.Parent := Scroll;  // Scroll is a scrollbox
 L.Top := 16;  
 L.Left := 60;
 L.Caption := 'Name';
 LNames.Add(L);
 end;
end;


2 Questions...
Note: If I try to LNames.Clear I get a Access Violation.
      I do destroy the list at Form1.Destroy

1)How do I get change the values of the labels?
-- LNames[0].Caption = 'Somthing else'; // wrong

2)How do I release any or all of the Label?
-- LNames[9].Free   // wrong

Don
0
Comment
Question by:d32coder
[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
  • 3
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
nnbbb09 earned 800 total points
ID: 6291100

Where do you create LNames? Has it been created by the time you call clear. Just inspect LNames and see if it is still set to nil.

1. To set the caption of a label in the list you need to typecast the pointer to a TLabel. eg : TLabel(LNames[0]).Caption := 'Something else';

2. If you are using a TList then you need to type the pointer to a TLabel before calling free. eg : TLabel(LNames[0]).Free; Also remember to delete the pointer (LNames.Delete(0)) after freeing.

If you have D4 onwards then you could consider using a TComponentList as this has the ability to own its objects, so that when you use clear, delete, free etc.. it frees the appropriate objects. It makes life a lot easier that using a raw TList.

Jo
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6291124
dear d32coder,

this code has not error but i dont can help u or not?

procedure TForm1.Button1Click(Sender: TObject);
var L: TLabel; I: Integer;
begin
  try
    LNames := TList.Create;
    LNames.Clear;
    for I := 0 to 9 do
    begin
      L := TLabel.Create(Form1);
      L.Parent := ScrollBar1;  // Scroll is a scrollbox
      L.Top := 16;
      L.Left := 60;
      L.Caption := 'Name';
      LNames.Add(L);
    end; // for


    // for Caption u must use (LNames.Items[i]) but i dont know it exactly


    LNames.Clear;


    //   just do all of ur needs here
    //   ..
    //   ..
  finally
    LNames.Destroy;
  end;

end;

best regards
hamed
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6291133
nnbbb09 code helped me:
TLabel(LNames[0]).caption := 'hamed' is true or at least
better than (LNames.Items[i]).

hamed
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:d32coder
ID: 6291234
Lnames is declared as a Global and is created in Form.Create.

I'm trying your code now.

bbiab
0
 

Author Comment

by:d32coder
ID: 6291559
I liked the TComponentList thing.

If we can change the question to that topic I'd appreciate it.

I'm trying this...  CList is my TComponentList and is global.  Created in Form.Create and Freed in Form.Destroy


procedure...
var L: TLabel;
begin
L := TLabel.Create(Form1);

L.Caption := 'a';
CList.Add(L);
L.Caption := 'b';
CList.Add(L);
end;

I know this is wrong because the pointers in the CList point to the same component...



My goal create as many Labels as needed at runtime (unknown number at design time but it could be as many as 100).  

I need to be able to edit the properties of each label ..
TLabel(CList[a]).Caption := 'c';

...and to remove them all when needed.
for i := 0 to CList.Count -1 do Clist.Remove(CList.First);

Having said all of that...


Please show me how to declare L so that I can add multiple instances of it insterad of overwriting the previous instance with a second call to CList.Add...


Even Better..  CList.Add(TLabel.Create(form1)); ???
This is becoming more complicated so I'll up the points.

Thanks to all you for being so patient.

Don



0
 

Author Comment

by:d32coder
ID: 6292133
I figured out the ComponentList but would have never explored it without your comment.

Thanks
Don
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
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 this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

664 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