前两天在论坛碰到这问题
form1上有12个Tlabel,要分别给caption赋值'1','2'..'12',有朋友的解答为:var
AComp: TComponent;
begin
for I := 1 to 12 do
begin
AComp := FindComponent('label' + IntToStr(I));
if AComp is TLabel then
TLabel(AComp).Caption := inttostr(i);
end;
end;请问如果Tlabel的默认的name值已经被修改,
即是不用FindComponent函数能不能实现要求?或者Tlabel的个数不定,又应该怎实现?
form1上有12个Tlabel,要分别给caption赋值'1','2'..'12',有朋友的解答为:var
AComp: TComponent;
begin
for I := 1 to 12 do
begin
AComp := FindComponent('label' + IntToStr(I));
if AComp is TLabel then
TLabel(AComp).Caption := inttostr(i);
end;
end;请问如果Tlabel的默认的name值已经被修改,
即是不用FindComponent函数能不能实现要求?或者Tlabel的个数不定,又应该怎实现?
i,j:Integer;
begin
j:=0;
for i:=0 to Self.ComponentCount-1 do
begin
if Self.Components[i] is TLabel then
begin
TLabel(Self.Components[i]).Caption:='label'+IntToStr(j);
Inc(j);
end;
end;
end;
i,j:Integer;
begin
j:=0;
for i:=0 to Self.ComponentCount-1 do //或者
//for i:=0 to Self.ControlCount-1 do
begin
if Self.Components[i] is TLabel then
begin
TLabel(Self.Components[i]).Caption:=IntToStr(j);
Inc(j);
end;
end;
end;
另:如果确实N值,而且TLabel控件的默认Name值未改变,以下写法可实现
var
AComp: TComponent;
begin
for I := 1 to 10 do
begin
AComp := FindComponent('label' + IntToStr(I));
if AComp is TLabel then
TLabel(AComp).Caption := inttostr(i);
end;
end;