http://topic.csdn.net/u/20080317/17/67137d08-73f6-4724-8ba9-18cd54bd89d1.html?81903143
这个贴子里,修改了之后单击button会提示“A component named button3 already exists”,请问怎样方便的Free或Destroy所有的已创建的button?以避免这个错误。谢谢

解决方案 »

  1.   

    建立一个button数组,然后根据这个数组进行销毁button
      

  2.   

    Button1:=FindComponent('Button1');//先根据名称进行寻找,如果找到,则不需要重新建立
    if Button1=nil then
      Button1:=TButton.Create(Self); //在这里建立新的
      
     //再根据属性进行设置其他参数
      

  3.   

    For i := ComponentCount - 1 downto 0 do begin
      if SameText(Components[i].ClassName, 'TButton') then
        Components[i].Free;
    end;
      

  4.   

    3楼 你没明白我的意思,因为修改了button的参数(比如宽度),是要使之立马生效的,所以不能说已存在了就不创建了,准确的说就是如何快速方便的替换?就跟Windows替换同名文件似的。
    当button多的时候我试着一个一个的Free掉 但太浪费时间了 还不如重启快,有没有好的办法呢?前提是不能让程序重启
      

  5.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      Buttontemp:TButton;
    begin
      Buttontemp:=TButton(FindComponent(Edit1.Text));
      if Buttontemp<>nil then
      begin
        Buttontemp.Width:=strtointdef(Edit2.Text,75);
        Buttontemp.Height:=strtointdef(Edit3.Text,25);
      end
      else
      begin
        Buttontemp:=TButton.Create(Self); //在这里建立新的
        Buttontemp.Name:=Edit1.Text;
        Buttontemp.Left:=100;
        Buttontemp.Top:=200;
        Buttontemp.Width:=strtointdef(Edit2.Text,75);
        Buttontemp.Height:=strtointdef(Edit3.Text,25);
        Buttontemp.Parent:=self;
      end;
    end;
      

  6.   

    顶上去,举个简单的例子
    在窗体上就建button1、button2、button3Button2Click的代码是Button1.Free;
    Button3Click的代码是Button1.Create(self);按button2后button1可以消失,但为什么按button3的时候会出错?求教各位高手,应该如何修改,谢谢。
      

  7.   

    Button3Click的代码是 Button1:=TButton.Create(self);
     Button1.Parent:=self;
     Button1.Caption:='Button1';
     Button1.Left:=0;
     Button1.Top:=0;如何有销毁Button1呢?改变其属性,就可以立即生效。如在Button2的Click中写
     button1.width:=100;
     button2.height:=200;
    难道button1的宽高没有改变吗?
      

  8.   

    wywry 说的已经很详细了,楼主自己应该先搞清楚 类与类实例的区别.主要还是楼主基础知识比较薄弱.
      

  9.   

    回楼上两位,问题是button是动态创建的,数量也不确定,而且要保持button与button之间的间距,所以改起来很麻烦,楼上两位如果有空的话加MSN,我把源码传给你们看看