如果我的程序窗口上有5个Label控件,其中有一个Label控件的Caption值为'abc'
请问:如何使用for语句或者其他的循环方式来逐个检查Label控件,最终找到那个
Caption值为'abc'的Label控件。
重点:如何用循环语句来实现多个相同控件的逐个检查!

解决方案 »

  1.   

    procedure TForm1.Button2Click(Sender: TObject);
    var
      i : Integer;
    begin
      for i := 0 to self.ComponentCount-1 do
        if self.Components[i] is TLabel then
          (self.Components[i] as TLabel).Caption := 'abc';
    end;
      

  2.   

    给你一个例子,是在FORM1放入N个Radiobutton,然后点击哪个就哪个字体变色:
    procedure tform1.aa;
    var
    i:integer;
    s:string;
    begin
    for i := 0 to self.componentcount-1 do
    if self.components[i] is TradioButton then
      if (self.components[i] as TradioButton).checked then
        begin
           (self.components[i] as TradioButton).Color :=clskyblue;
        end
        else
           (self.components[i] as TradioButton).Color :=clBtnFace;
    end;
    请别忘记声明,使用时,在每个TradioButton的click事件中调用这个过程就行了
      

  3.   

    var
      I: Integer;
    begin
      for I := 0 to ControlCount -1 do
        if Controls[i].ClassNmae = Tlabel then
           with Tlabel(Controls[i]) do
             if caption='abc' then
               showmessage(caption);
      

  4.   

    var i:integer;
    str:string;
    begin
      for i:=0 to componentcount-1 do
        if (components[i] is TLabel)and((components[i] as TLabel).Caption='abc') then
          begin
            str:=(components[i] as TLabel).Name;
            showmessage(str);
          end;
      

  5.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      i : Integer;
    begin
      for i := 0 to self.ComponentCount-1 do
        if self.Components[i] is TLabel then
         if (self.Components[i] as TLabel).Caption <> 'abc' then
         begin
           (self.Components[i] as TLabel).Visible:=false;
         end;
    end;