d32coder
asked on
RunTime Labels
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
nnbbb09 code helped me:
TLabel(LNames[0]).caption := 'hamed' is true or at least
better than (LNames.Items[i]).
hamed
TLabel(LNames[0]).caption := 'hamed' is true or at least
better than (LNames.Items[i]).
hamed
ASKER
Lnames is declared as a Global and is created in Form.Create.
I'm trying your code now.
bbiab
I'm trying your code now.
bbiab
ASKER
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(fo rm1)); ???
This is becoming more complicated so I'll up the points.
Thanks to all you for being so patient.
Don
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(fo
This is becoming more complicated so I'll up the points.
Thanks to all you for being so patient.
Don
ASKER
I figured out the ComponentList but would have never explored it without your comment.
Thanks
Don
Thanks
Don
this code has not error but i dont can help u or not?
procedure TForm1.Button1Click(Sender
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