例如有checkbox1,checkbox2.......checkbox100
能否使用一个循环来一次性对这些CHECKBOX控件的Checked属性为TRUE;请给个例子,谢谢!

解决方案 »

  1.   

    注意TCheckBox是放在窗体上。procedure TForm1.Button1Click(Sender: TObject);
    var
      i: Integer;
    begin
      for i:= 0 to Self.ComponentCount - 1 do
        if Self.Components[i] is TCheckBox then
          (Self.Components[i] as TCheckBox).Checked := True;
    end;
      

  2.   

    谢谢!Dlwxn(Dlwxn)可是我的窗体上还有好多其他的控件,
    所以这样不好办
    是否可以用这个:Tcheckbox(FindComponent('checkbox'+IntToStr(h))).Checked:=true
    但是执行到这句时就出错。
      

  3.   

    不是已经判断了:if Self.Components[i] is TCheckBox then  //如果是TCheckBox则执行,其它的不赋值。
      

  4.   

    谢谢!Dlwxn(Dlwxn)可是我的窗体上还有好多其他的控件,
    所以这样不好办
    是否可以用这个:Tcheckbox(FindComponent('checkbox'+IntToStr(h))).Checked:=true
    但是执行到这句时就出错。
    -----------------
    用FindComponent('checkbox'+IntToStr(h))适合你的checkbox的name是checkbox1 checkbox2 ...有规律的情况下,而Dlwxn给出的代码是判断控件类型,所以后者要比前者好一些至于你说的出错,应该是某个checkbox控件没有按规律命名,程序无法找到造成的
      

  5.   

    CHECKBOX控件命名是有规律的。执行每一个的时候就出错了,应该是提示没找到吧,可是这个控件是有的,不知是否还有其他的语句没有写到
      

  6.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      I: Integer;
    begin
      for I := 1 to 100 do
        if FindComponent('CheckBox' + IntToStr(I)) <> nil then
          TCheckBox(FindComponent('CheckBox' + IntToStr(I))).Checked := True;
    end;
      

  7.   

    if FindComponent('CheckBox' + IntToStr(I)) <> nil then
           TCheckBox(FindComponent('CheckBox' + IntToStr(I))).Checked := True;是检查是否存在这个控件吧,可是我那控件明明是存在的,可以就是无法检测到,
     
    我把它改成这样,还是没有,而且肯定在窗体有CheckBox1   if FindComponent('CheckBox1') <> nil then
           TCheckBox(FindComponent('CheckBox1').Checked := True;
      

  8.   

    Tcheckbox(self.FindComponent('checkbox'+IntToStr(h))).Checked:=true         这里加上self
      

  9.   

    Tcheckbox(self.FindComponent('checkbox'+IntToStr(h))).Checked:=true
               |
             这里加上self
      

  10.   

    非常感谢  hellolongbin(一个人[终于换工作了]) 
    按照您的方法问题解决了,再次感谢!^_^