在窗体上有
checkbox1,checkbox2,checkbox3,checkbox4,checkbox5
label1,label2,label3,label4,label5这些控件
想用一个循环实现
for i:=1 to 5 do
 begin
 if checkbox[i].checked=true then;//这里如何写
       begin
        label[i].caption:='0';//这里如何写
       end;
 end;
万分感谢!

解决方案 »

  1.   

    for i:=1 to 5 do
     begin
     if (checkbox[i] as CheckBox).checked=true then;//这里如何写
           begin
            (label[i] as Label).caption:='0';//这里如何写
           end;
     end;
      

  2.   

    看一下,findcomponents 的帮助信息,再领悟一下就是了!
      

  3.   

    for I := 1 to 5 do
        begin
         if TCheckBox(self.FindComponent('checkbox'+inttostr(i))).Checked=true then
    .....
       end;
      

  4.   

    for I:= 0 to Form1.ControlCount -1 do
    begin
      if pos('checkbox',form1.controls[i].name)>0 and (form1.controls[i] is TCheckBox) then
      if (form1.controls[i] as TCheckBox).checked then
      begin
        for j:=0 to Form1.controlCount-1 do
        begin
          if pos('Label',form1.controls[i].name)>0 and (form1.controls[i] is TLabel) then
          (form1.controls[j] as TLabel).Caption:='0'
        end;
      end;
    end;
      

  5.   

    if Tcheckbox(self.FindComponent('checkbox'+inttostr(i))).checked=true
      

  6.   

    问题不明确,窗体上是否还有Tcheckbox,和Tlabel 控件,
    如果没有就可以用 xwc21s() 的方法;
    没有的话就要判断是否是你要改变的;
      

  7.   

    procedure TForm1.BitBtn1Click(Sender: TObject);
    var i : integer;
    begin  for i:=0 to form1.componentcount -1 do begin
        if (form1.Components[i] is TCheckBox) then begin
          if TCheckBox(form1.Components[i]).Checked then
          TLabel(form1.FindComponent('Label'+Copy((form1.Components[i]).Name,Length((form1.Components[i]).Name),1))).Caption := 'OK';
         // showmessage(Copy((form1.Components[i]).Name,Length((form1.Components[i]).Name)-1,1)+(form1.Components[i]).Name);
        end;
      end;
    end;//要保证checkbox和label的标号一致哦,
      

  8.   

    for I:= 0 to 5 do
    begin
      if (findcomponents('CheckBox'+inttostr(i)) as TCheckBox).checked then
       (findcomponents('Label'+inttostr(i)) as TLabel).Caption:='0';
    end;
      

  9.   

    这个好像用tag属性就能够辨别了
      

  10.   

    var
    i: integer
    begin
     for I:= 0 to 5 do
     begin
      if (findcomponents('CheckBox'+inttostr(i)) as TCheckBox).checked then
       (findcomponents('Label'+inttostr(i)) as TLabel).Caption:=inttostr(i);
     end;
    end;
      

  11.   

    to:woshiwo(我是我)
    你的代码我试过,不行. 
    我试过这样,也不行
    var
      vComponent,vComponent1: TComponent;begin
      for I := 1 to 15 do 
      begin
        vComponent := FindComponent('CheckBox' + IntToStr(I));
        vComponent1 := FindComponent('label' + IntToStr(I));
        if Assigned(vComponent) then
             if  TCheckBox(vComponent).checked = true then
             if Assigned(vComponent1) then
          Tlabel(vComponent1).caption := '1';
      end;
    end;还望大家指出
      

  12.   

    if Tcheckbox(self.FindComponent('checkbox'+inttostr(i))).checked=true
    Black(小熊)的方法最好。应该得100分:)
      

  13.   

    to:yansea(思宏) (  )
    你的方法已经可以了,不过为什么这样的代码不行呢?
    var
    i: integer;
    begin
     for I:= 0 to 5 do
     begin
      if (findcomponents('CheckBox'+inttostr(i)) as TCheckBox).checked then
       (findcomponents('Label'+inttostr(i)) as TLabel).Caption:=inttostr(i);
     end;
    end;
      

  14.   

    to:Black(小熊) 
    你的方法也可以,而且比 yansea(思宏)的要好一些
    yansea(思宏)的方法是如果checkbox6,和label6的话,也是一样了!
      

  15.   

    if Tcheckbox(components[i]).checked = true then……