Delphi 更改标签标题不起作用
Delphi changing label caption does not work
所以我需要在我的表单上找到一些动态创建的 TLabel 组件,并更改它们的标题,但我一定是做错了什么。请检查下面的代码并帮助我,我只是不知道还能尝试什么:
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;
我根本不明白这个问题
如果我只是尝试这个代码:
lbx:=TLabel.Create(self);
lbx:=FindComponent('Layernumber1') as TLabel;
lbx.Caption:='jkjkghgkjghk';
它就像一个魅力...
那我做错了什么?
请...
编辑
我在代码中添加了备注行,因此:
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;
我运行面板的OnMouseUp事件中的程序
当 运行ning:
时,我的备忘录看起来像这样
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
如您所见,'Done' 行表示代码正在执行
(Mainform.Components[j] as TLabel).Caption := IntToStr(ordin);
这将更改该标签控件的标题。由于您报告代码无法更改标签,我们只能得出结论,此代码未执行,或者它在错误的标签上执行。
您可以通过单步执行代码来缩小问题范围。首先在这一行设置一个断点:
if StartsText('Layernumber',mainform.Components[j].Name) then
如果该断点未触发,则 mainform
拥有的组件中 none 的类型为 TLabel
。
然后继续
if mainform.Components[j].GetParentComponent = pan then
如果那里的断点没有触发,则 mainform
拥有的标签中 none 的名称以 'Layernumber'
.
开头
等等。您可以使用此技术来了解为什么没有执行对 Caption
的分配,或者为什么它在错误的组件上执行。
这段代码当然读起来很可怕。你当然可以做得更好。为什么创建标签然后忘记它。记住在表单变量中对标签的引用,让自己的生活变得简单。
FLabelNumber1 := TLabel.Create(Self);
...
那你可以把题中的代码全部扔掉,直接用FLabelNumber1
。
为了给您更多鼓励,我可以诚实地说,在交付 Delphi 软件超过 15 年的时间里,我从未交付过调用 FindComponent
.
的代码
顺便说一句,您几乎可以肯定不应该使用 mainform
全局变量。您的代码在 TmainForm
的方法中执行。因此它已经可以通过隐式 Self
变量访问 object 形式。请花时间删除 mainform
.
的所有这些用途
var
i: integer;
begin
for i := 0 to pan.ControlCount -1 do
begin
if (pan.Controls[i] is TLabel) and
(pan.Controls[i].Name = 'YOURLABEL' + IntToStr(i)) then
TLabel(pan.Controls[i]).Caption := 'Do whatever you want';
end;
end;
所以我需要在我的表单上找到一些动态创建的 TLabel 组件,并更改它们的标题,但我一定是做错了什么。请检查下面的代码并帮助我,我只是不知道还能尝试什么:
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;
我根本不明白这个问题 如果我只是尝试这个代码:
lbx:=TLabel.Create(self);
lbx:=FindComponent('Layernumber1') as TLabel;
lbx.Caption:='jkjkghgkjghk';
它就像一个魅力... 那我做错了什么?
请...
编辑
我在代码中添加了备注行,因此:
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;
我运行面板的OnMouseUp事件中的程序 当 运行ning:
时,我的备忘录看起来像这样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
如您所见,'Done' 行表示代码正在执行
(Mainform.Components[j] as TLabel).Caption := IntToStr(ordin);
这将更改该标签控件的标题。由于您报告代码无法更改标签,我们只能得出结论,此代码未执行,或者它在错误的标签上执行。
您可以通过单步执行代码来缩小问题范围。首先在这一行设置一个断点:
if StartsText('Layernumber',mainform.Components[j].Name) then
如果该断点未触发,则 mainform
拥有的组件中 none 的类型为 TLabel
。
然后继续
if mainform.Components[j].GetParentComponent = pan then
如果那里的断点没有触发,则 mainform
拥有的标签中 none 的名称以 'Layernumber'
.
等等。您可以使用此技术来了解为什么没有执行对 Caption
的分配,或者为什么它在错误的组件上执行。
这段代码当然读起来很可怕。你当然可以做得更好。为什么创建标签然后忘记它。记住在表单变量中对标签的引用,让自己的生活变得简单。
FLabelNumber1 := TLabel.Create(Self);
...
那你可以把题中的代码全部扔掉,直接用FLabelNumber1
。
为了给您更多鼓励,我可以诚实地说,在交付 Delphi 软件超过 15 年的时间里,我从未交付过调用 FindComponent
.
顺便说一句,您几乎可以肯定不应该使用 mainform
全局变量。您的代码在 TmainForm
的方法中执行。因此它已经可以通过隐式 Self
变量访问 object 形式。请花时间删除 mainform
.
var
i: integer;
begin
for i := 0 to pan.ControlCount -1 do
begin
if (pan.Controls[i] is TLabel) and
(pan.Controls[i].Name = 'YOURLABEL' + IntToStr(i)) then
TLabel(pan.Controls[i]).Caption := 'Do whatever you want';
end;
end;