很多TLabel控件,如何根据其name属性,快速定位该控件并更改其颜色属性。多谢了!

解决方案 »

  1.   

    Tlabel(findcomponent('ctrlname')),caption := '';
      

  2.   

    Tlabel(findcomponent('ctrlname')).color = ......
      

  3.   

    若知其Name,如Label2,直接Label2.Color := ...就可以了;
    若只知其Name的字符串,用下面方法试试,遍历组件
    用下面方法先找到再转化,你可依此找出其它的;如Edit等和其类似:
    if self.FindComponent('Label2') <> nil then
      TLabel(self.FindComponent('Label2')).Caption := 'Label2';
      

  4.   

    每个label生成的时候都定义好tag值,用它的tag映射一个函数来决定位置
      

  5.   

    procedure TBasicForm.Enable;
    var
       I :integer;
    begin
      for I := 0 to ComponentCount - 1 do begin
        if  (Components[I] is TLabel) then
           begin
              if  (Tlable(Components[I]).name ='lable1')  then
                 Tlable(Components[I]).color :=10;
            end;
    end;
      

  6.   


    //这个通过。你试试。。procedure TForm1.Button1Click(Sender: TObject);var
       I :integer;
    begin
      for I := 0 to ComponentCount - 1 do begin
        if  (Components[I] is TLabel) then
           begin
              if  (TLabel(Components[I]).name ='Label1')  then
                 TLabel(Components[I]).color :=clskyblue;
            end;
    end;
      

  7.   

    procedure TForm1.Button1Click(Sender: TObject);var
       I :integer;
    begin
      for I := 0 to ComponentCount - 1 do 
    begin
        if  (Components[I] is TLabel) then
           begin
              if  (TLabel(Components[I]).name ='Label1')  then
                 TLabel(Components[I]).color :=clskyblue;
            end;
    end;
    end;