我现在有30个组件,都是TLabel,但是全部都被我重命名了。我把30个TLabel分成两组,分别是 A1、A2、A3……A15 和 B1、B2、B3……B15,
我现在想要对这30个TLabel的Caption属性进行赋值,请问循环语句如何写?如果可以,请尽量使用For语句,代码力求简洁,有些地方最好附上注释,菜鸟我感激不尽!!

解决方案 »

  1.   

    for m:=0 to  Self.ComponentCount -1   do 
        if Self.Componets[m]   is   TLabel   then
          TLabel(Self.Componets[m]).Caption:='都是一个名字';
    //其他的自己想象一下~~~比如
    TLabel(Self.Componets[m]).top
    TLabel(Self.Componets[m]).left 等等,反正你想改啥就改啥
      

  2.   

    2楼的方法提示构建错误
    [错误] Unit2.pas(268): Undeclared identifier: 'Componets'
    [错误] Unit2.pas(268): Operator not applicable to this operand type
    [错误] Unit2.pas(269): Undeclared identifier: 'Componets'
      

  3.   


    var
      I: Integer;
      Button: TControl;
    begin
      for I := 0 to ControlCount - 1 do
      begin
        Button := nil;
        Button := FindChildControl('A' + IntToStr(I));
        if Assigned(Button) then
          TLabel(Button).Caption := 'Label' + IntToStr(I);
      end;
    end;
      

  4.   

    把ControlCount  改成你要查的A,B最大值 
      

  5.   

    请问能够不用Button吗?我看不太明白3楼提供的代码
      

  6.   


    var
      I: Integer;
      Button: TComponent;
    begin
      for I := 0 to 20 do
      begin
        Button := nil;
        Button := FindComponent('A' + IntToStr(I));
        if Assigned(Button) then
          TLabel(Button).Caption := 'Label' + IntToStr(I);
      end;
    end;开始写错了。 FindChildControl只是TWINCONTROL继承下来的控件的查找
      

  7.   

    测试成功了,不过请问一下,Component是什么?if Assigned(Button) then这句又是什么意思?
      

  8.   

    Component是一个 List
    是一个窗口记录属于他的控件列表Assigned(button)
    是如果button是存在的
      

  9.   

    Component说错了,不是List,是一个类,Vcl控件都有继承他2楼的不是错的,是你用错了地方每个窗口有个列表,保存属于他上面的控件那个方式是遍历所有的控件,查找你要的那个类型