So I need to locate some dynamicly created TLabel components on my form, and change the caption for them but I must be doing something wrong. Please check the code bellow and help me out, I just don't know what else to try:
procedure TmainForm.setLabelCaptionForPanel(pan:TPanel; ordin: integer);
var
j:integer;
begin
for j := 0 to mainform.ComponentCount - 1 do
begin
if mainform.Components[j] is TLabel then
if StartsText('Layernumber',mainform.Components[j].Name) then
begin
if mainform.Components[j].GetParentComponent = pan then
begin
(Mainform.Components[j] as TLabel).Caption := IntToStr(ordin);
end;
end;
end;
end;
I simply do not understand the problem If I simply try this code:
lbx:=TLabel.Create(self);
lbx:=FindComponent('Layernumber1') as TLabel;
lbx.Caption:='jkjkghgkjghk';
It works like a charm... So what am I doing wrong ?
Please...
EDIT
I added the memo lines to the code so:
procedure TmainForm.setLabelCaptionForPanel(pan:TPanel; ordin: integer);
var
j:integer;
lbx:TLabel;
begin
memo1.lines.Add('Setting layer lbl caption on panel:'+pan.name+', to:'+IntToStr(ordin));
//lbx:=TLabel.Create(self);
//lbx:=FindComponent(vt.FieldValues['Layernumber']+) as TLabel;
for j := 0 to ComponentCount - 1 do
begin
if Components[j] is TLabel then
if StartsText('Layernumber',Components[j].Name) then
begin
Memo1.Lines.Add('Component label found:'+mainform.Components[j].Name);
Memo1.Lines.Add('Having parent:'+TLabel(Mainform.Components[j]).Parent.Name);
if Components[j].GetParentComponent = pan then
begin
Memo1.Lines.Add('Labem found (name):'+Components[j].Name+' with caption:'+TLabel(Components[j]).Caption);
Memo1.Lines.Add('Has parent:'+pan.Name);
// Memo1.Lines.Add('xxx...:'+TLabel(Mainform.Components[j]).name+' - '+TLabel(Mainform.Components[j]).Caption+' -> '+IntToStr(ordin));
(Components[j] as TLabel).Caption := IntToStr(ordin);
Memo1.Lines.Add('Done');
end;
end;
end;
end;
I run the procedure in an OnMouseUp event of a panel And my memo looks like this when running:
Senderul:Layeru0
Searching on panel:Layeru0
Panelul:Layeru0 - cu captionul obtinut=0
GetNumar de layere=3
Layer panel gasit este:Layeru3
Setting layer lbl caption on panel:Layeru3, to:3
Component label found:Layernumber0
Having parent:Layeru0
Component label found:Layernumber1
Having parent:Layeru1
Component label found:Layernumber2
Having parent:Layeru2
Component label found:Layernumber3
Having parent:Layeru3
Labem found (name):Layernumber3 with caption:3
Has parent:Layeru3
Done
Layer panel gasit este:Layeru0
Setting layer lbl caption on panel:Layeru0, to:2
Component label found:Layernumber0
Having parent:Layeru0
Labem found (name):Layernumber0 with caption:0
Has parent:Layeru0
Done
Component label found:Layernumber1
Having parent:Layeru1
Component label found:Layernumber2
Having parent:Layeru2
Component label found:Layernumber3
Having parent:Layeru3
Layer panel gasit este:Layeru2
Setting layer lbl caption on panel:Layeru2, to:1
Component label found:Layernumber0
Having parent:Layeru0
Component label found:Layernumber1
Having parent:Layeru1
Component label found:Layernumber2
Having parent:Layeru2
Labem found (name):Layernumber2 with caption:2
Has parent:Layeru2
Done
Component label found:Layernumber3
Having parent:Layeru3
Layer panel gasit este:Layeru1
Setting layer lbl caption on panel:Layeru1, to:0
Component label found:Layernumber0
Having parent:Layeru0
Component label found:Layernumber1
Having parent:Layeru1
Labem found (name):Layernumber1 with caption:1
Has parent:Layeru1
Done
Component label found:Layernumber2
Having parent:Layeru2
Component label found:Layernumber3
Having parent:Layeru3
As you may see the 'Done' line says the code is executing