有窗体上有20个combobox控件,置初值visible:=false;
因为打开本窗体时,根据传来的参数n,要显示不同数量的combobox,不知道如何控制?
for i:=1 to n do
  if self.Components[i] is Tcombobox then
    begin
    tcombobox(self.Components[i]).Visible:=true;
    tcombobox(self.Components[i]).Text:='';
    end;
n是传来的显示个数参数,结果当然是不正确的。
如果 n 改为Self.ComponentCount - 1 ,它又把所有的combobox处理了,不是我想要的。
如果改成顺序结构又好麻烦,请高手帮帮我。

解决方案 »

  1.   

    function TForm1.ShowCombox(n: integer): Boolean;
    Var
      i,ret:integer;
    begin
      ret := 0;  For i := 0 To Self.ComponentCount - 1 Do
      Begin
        If ret > n Then
          Exit;    If (self.Components[i] is Tcombobox) then
        Begin
          Tcombobox(self.Components[i]).Visible:=true;
          Tcombobox(self.Components[i]).Text:='';
        End;    ret := ret + 1;
      End;
    end;
      

  2.   

    这个应该可以实现,我觉得如果动态创建combobox更好些。
      

  3.   

    同意1楼,设一个局部变量,来记录是否找到n个控件即可,for循环要用ComponentCount 
      

  4.   

    加个计数的变量,每self.Components[i] is Tcombobox 就+1,达到n前Tcombobox的设true,达到后的Tcombobox设false
      

  5.   

    function TForm1.ShowCombox(n: integer): Boolean; 
    Var 
      i,ret:integer; 
    begin 
      ret := 0; 
      For i := 0 To Self.ComponentCount - 1 Do 
      Begin 
        If (self.Components[i] is Tcombobox) then 
        Begin 
          Tcombobox(self.Components[i]).Visible:=true; 
          Tcombobox(self.Components[i]).Text:=''; 
          ret := ret + 1;
          If ret > n Then 
          Exit; 
        End; 
      End; 
    end;
    改成这样就是我需要的了,谢谢了。