比如,有一个主form1,
上面有12个button1~button12;   现在有一个动态的条件(该条件是button上面的一个属性值,比如是caption='buttonOK',要找到符合条件的button。怎么办呢?
   我想到可以用一个什么东西在button组件上面遍历即可。。问题是怎么实现呢?

解决方案 »

  1.   

    var
    i:integer;
    begin
    for i:=0 to form1.ComponentCount-1 do
    begin
    if form1.Components[0] is tbutton then
    begin
      showmessage(tbutton(form1.Components[i]).caption);
    end;end;
    end;
      

  2.   

    Components是components类型的数组
    var
    i:integer;
    begin
    for i:=0 to form1.ComponentCount-1 do//从0到窗体上的控件总数-1做循环
    begin
    if form1.Components[i] is tbutton then//如果窗体的第i个控件是tbutton类型
    begin
      showmessage(tbutton(form1.Components[i]).caption);
    end;end;
    end;
      

  3.   

    with Form1 do
    begin
      for i:=0 to ComponentCount-1 do
      begin
        if (Components[i] is TButton) and (TButton(Components[i]).Caption='ButtonOK') then
          ...
      end;
    end;或者with Form1 do
    begin
      for i:=0 to ControlCount-1 do
      begin
        if (Controls[i] is TButton) and (TButton(Controls[i]).Caption='ButtonOK') then
          ...
      end;
    end;