比如有10个label控件,怎样用一个for循环来对这10个label统一操作,比如label1的显示1,label2显示2依此类推,用如下的语句编译不能通过,请问应该怎么写?
for i:=1 to 10 do
(label+i).caption:=inttostr(i);

解决方案 »

  1.   

    for i:=1 to 10 do
      (FindComponent('Label'+IntToStr(i)) as TLabel).Caption:=IntToStr(i);
      

  2.   

    你上面的语句当然通不过了,每一个标签都是一个TLabel的实例对象,它并不是变量,而是一个对象!
    你可以遍历所有窗体的Label嘛!用窗体的Components属性,得到所有窗体的控件,然后再判断当前得到的控件是不是TLable类!For count=o to Form1.Components.count-1 do
    begin
      if Form1.Components[count] is TLabel then
         
    end;这个前提是窗体中每一个标签都是你要处理的标签呢!
      

  3.   

    呵呵,不过相信楼主自己心里明白到底可不可以!function TComponent.FindComponent(const AName: string): TComponent;
    var
      I: Integer;
    begin
      if (AName <> '') and (FComponents <> nil) then
        for I := 0 to FComponents.Count - 1 do
        begin
          Result := FComponents[I];
          if SameText(Result.FName, AName) then Exit;
        end;
      Result := nil;
    end;