label1.caption:=1;
label2.caption:=1;
label3.caption:=1;
label4.caption:=1;
label5.caption:=1;
label6.caption:=1;
label7.caption:=1;
label8.caption:=1;
以上的我能不能用个i变量来控制label,如下
for i:=1 to 8 do
  begin
   labeli.caption:=1;
end;
若不行,要怎么处理?

解决方案 »

  1.   

    for i:=1 to 8 do
      begin
       TLabel(FindComponent('label'+inttostr(i))).Caption:='1';
    end;
      

  2.   

    可以是可以啊,不过你要定义label数组而不是象你写的那样
      

  3.   

    var
      i,j:integer;
    begin
      i:=form1.ComponentCount;
      if i>0 then
      begin
        for j:=0 to i-1 do
        begin
          if form1.Components[j].ClassName='TLabel' then
          begin
            (form1.Components[j] as TLabel).Caption:='1';
          end;
        end;
      end;
    end;
      

  4.   

    With TLabel(Self.FindComponents('Label') + IntToStr(i)) do
      ..........----------------
    沉沦中..........
      

  5.   

    var
      AComponent:TComponent;
    for i:=1 to 8 do
      begin
        AComponent:=FindComponent('Label'+IntToStr(i));
        if AComponent is TLabel then
           TLabel(AComponent).Caption:='caption';
      end;