TForm 有一个Components 属性 记录了 Form所拥有的组件 你可以遍历这个属性,得到你要Free的对象。

解决方案 »

  1.   

    procedure TForm1.Button3Click(Sender: TObject);
    var
      I:integer;
    begin
      for i:=0 to ComponentCount-1 do
      begin
        if Components[i].ClassName='TLabel' then
        begin
          ShowMessage(Components[i].Name);
          (FindComponent(Components[i].Name) as TLabel).Free;
        end;
        if Components[i].ClassName='TShape' then
        begin
          ShowMessage(Components[i].Name);
          (FindComponent(Components[i].Name) as TShape).Free;      
        end;
      end;
    end;
      

  2.   

    同意楼上的,
    用TAG等来标记
    然后再用狼兄的方法加上一个判断TAG值就可以
      

  3.   

    to:tikkypeng(千两狂死郎)
    你的方法確實不錯,但這樣會把form1中的所有Tshape及Tlabel清除掉。
    可是我只是想清除form1中Panel1控件內的所有控件,而未包含在Panel1中的控件均不需要清除,怎么辦?
      

  4.   

    那你判断一下
    Label的Parent是不是Panel1阿~~~~不就行了??
      

  5.   

    to: tikkypeng(千两狂死郎) 
    我剛試過了但用for i:=0 to (ComponentCount-1)時提示以下錯誤:<注:此時在panel中加入了2個label,2個shape>
    List index out of bounds(4)
    為什么?您有沒有測試過?
      

  6.   

    to: tikkypeng(千两狂死郎) unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, OWEdit, ExtCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Panel1: TPanel;
        Shape1: TShape;
        Shape2: TShape;
        Label1: TLabel;
        Label2: TLabel;
        Label3: TLabel;
        Shape3: TShape;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var
      I:integer;
    begin
      for i:=0 to (ComponentCount-1) do
      begin
        if Components[i].ClassName='TLabel' then
        begin
          (FindComponent(Components[i].Name) as TLabel).Free;
        end;
        if Components[i].ClassName='TShape' then
        begin
          (FindComponent(Components[i].Name) as TShape).Free;
        end;
      end;
    end;end.
      

  7.   

    to: tikkypeng(千两狂死郎) 
    看來問題有點繁!!!
    再加30分!!!
      

  8.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      I:integer;
      j:integer;
    begin
      for i:=ComponentCount-1 downto 0 do
      begin
        if Components[i].ClassName='TLabel' then
        begin
         Components[i].Free;
        end;
      end;
      for i:=ComponentCount-1 downto 0 do
      begin
        if Components[i].ClassName='TShape' then
        begin
         Components[i].Free;
        end;
      end;   
    end;
      

  9.   

    sundayboys告诉我的阿!!呵呵~~
      

  10.   

    我自己解决这个问题是做一个管理机制,首先在form上建立一个tstringlist,每增加一个对象就在stringlist里登记,addobject方法,这样把对象掌握在自己控制之下,当某个对象free的时候,查表,然后free,并delete,一劳永逸的工作和技巧。当然命名规则可以自动或者手动。在许多地方还有需要特殊作用的时候再去使用tag.
      

  11.   

    scripting的方法不错,我还喜欢这种解决方案。
      

  12.   

    因为你在Free掉控件的同时,引起了Component的改变,实际上就如同在链表中删掉了一个元素,
    而i还是要按原来的轨迹循环,所以当然要越界,以下是我的做法,仅供交流:procedure TForm1.Button1Click(Sender: TObject);var
      I:integer;
    begin
      I := 0;
      while(i<ComponentCount) do
      begin
        if Components[i].ClassName='TLabel' then
        begin
          (FindComponent(Components[i].Name) as TLabel).Free;
        end
        else if Components[i].ClassName='TShape' then
        begin
          (FindComponent(Components[i].Name) as TShape).Free;
        end
        else
          inc(i);
      end;
      Update();
    end;
      

  13.   

    to: tikkypeng(千兩狂死郎)
        你上面的方法確實可以了,沒有錯誤提示,但還是有一點問題就是不能保証所刪
    除控件的Parent為Panel1,我試著用你前面講的方法判斷Components[i].Parent屬性
    但又錯了,因為Components[i]根本沒有parent屬性。所以我在動態加入每一個控件時都將其tag屬性設為3然后再用Components[i].tag=3作
    判斷,則可以一次刪除。
    但我不知道tag屬性到底是作什麼用的,它的默認值為0。能否解釋一下?
      

  14.   

    Stores an integer value as part of a component. 
    TComponent.TagTComponent Exampleproperty Tag: Longint;DescriptionTag has no predefined meaning. The Tag property is provided for the convenience of developers. It can be used for storing an additional integer value or it can be typecast to any 32-bit value such as a component reference or a pointer.The following example enables the user to move the current selected cell in a db grid.  The Up and Down buttons have their OnClick events assigned to the UpDownClick procedure.  The Left and Right buttons have their OnClick events assigned to the LeftRightClick procedure.  The Up and Left buttons have their Tag property set to ?, while the Down and Right buttons have their Tag property set to 1.procedure TForm1.UpDownClick(Sender: TObject);
    begin
      Table1.MoveBy(TComponent(Sender).Tag);
      DBGrid1.SetFocus;
    end;procedure TForm1.LeftRightClick(Sender: TObject);
    begin
      DBGrid1.SelectedIndex := DBGrid1.SelectedIndex + TComponent(Sender).Tag;
      DBGrid1.SetFocus;
    end;
      

  15.   

    kkypeng(千两狂死郎)的方法可以行得通。我也是用类似的方法处理的。
      

  16.   

    tikkypeng(千两狂死郎)是不是你的名字教NB阿
    佩服!!
    你的方法可以行的同的阿^_^