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;
to: tikkypeng(千两狂死郎) 我剛試過了但用for i:=0 to (ComponentCount-1)時提示以下錯誤:<注:此時在panel中加入了2個label,2個shape> List index out of bounds(4) 為什么?您有沒有測試過?
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.
to: tikkypeng(千两狂死郎) 看來問題有點繁!!! 再加30分!!!
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;
因为你在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;
to: tikkypeng(千兩狂死郎) 你上面的方法確實可以了,沒有錯誤提示,但還是有一點問題就是不能保証所刪 除控件的Parent為Panel1,我試著用你前面講的方法判斷Components[i].Parent屬性 但又錯了,因為Components[i]根本沒有parent屬性。所以我在動態加入每一個控件時都將其tag屬性設為3然后再用Components[i].tag=3作 判斷,則可以一次刪除。 但我不知道tag屬性到底是作什麼用的,它的默認值為0。能否解釋一下?
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;
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;
用TAG等来标记
然后再用狼兄的方法加上一个判断TAG值就可以
你的方法確實不錯,但這樣會把form1中的所有Tshape及Tlabel清除掉。
可是我只是想清除form1中Panel1控件內的所有控件,而未包含在Panel1中的控件均不需要清除,怎么辦?
Label的Parent是不是Panel1阿~~~~不就行了??
我剛試過了但用for i:=0 to (ComponentCount-1)時提示以下錯誤:<注:此時在panel中加入了2個label,2個shape>
List index out of bounds(4)
為什么?您有沒有測試過?
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.
看來問題有點繁!!!
再加30分!!!
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;
而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;
你上面的方法確實可以了,沒有錯誤提示,但還是有一點問題就是不能保証所刪
除控件的Parent為Panel1,我試著用你前面講的方法判斷Components[i].Parent屬性
但又錯了,因為Components[i]根本沒有parent屬性。所以我在動態加入每一個控件時都將其tag屬性設為3然后再用Components[i].tag=3作
判斷,則可以一次刪除。
但我不知道tag屬性到底是作什麼用的,它的默認值為0。能否解釋一下?
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;
佩服!!
你的方法可以行的同的阿^_^