我在一个函数中动态创建了一个按钮数组,最大值是10,可是这个函数要经常调用!每次创建的数目也不一样多!而在创建后,再调用就不行是因为这个控件名称已存在!要如何在再次调用函数时把原先创建的不知个数的按钮删掉!我建的是b1,b2等!

解决方案 »

  1.   

    调用free不就行了
    可以用Assigned测试是否为nil
      

  2.   

    for i:=1 to 10 do begin
          if Assigned(s[i]) then begin
              s[i].Free;
              s[i]:=nil;
          end;
      end;
    这个为什么不行?
      

  3.   

    for i:=ComponentCount downto 1 do
    begin
      if Components[i].ClassNameIs('TButton') then
      TButton(Components[i]).Free;
    end;
      

  4.   

    for i:=1 to 10 do begin
          if Assigned(s[i]) then begin
              s[i].Free;
              s[i]:=nil;
          end;
      end;
      

  5.   

    for i:=1 to 10 do 
      if Assigned(s[i])then FreeAndNil(s[i]);
      

  6.   

    那你就用findcomponent(“b1”)等去找,找到了就释放了
      

  7.   

    for i:=1 to 10 do 
      if TButton(s[i])<>nil then 
      begin
        TButton(s[i]).Free;
        TButton(s[i]):=nil;
      end;
      

  8.   

    while (Form1.componentcount<>0) and (判断是按钮类型) do
      Form1.components[0].free;