about 動態產生元件 |
答題得分者是:william
|
fanny
一般會員 發表:10 回覆:26 積分:7 註冊:2002-07-19 發送簡訊給我 |
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
|
Miles
尊榮會員 發表:27 回覆:662 積分:622 註冊:2002-07-12 發送簡訊給我 |
Hello fanny try this:
全部釋放 for i := 0 to Form1.ComponentCount - 1 do Form1.Components[i].Free;若只是要釋放動態產生的元件就用TList去紀錄 var MyList : TList; procedure TForm1.FormCreate(Sender: TObject); begin MyList := TList.Create; end; procedure TForm1.Button2Click(Sender: TObject); var MyComponent : TEdit; begin MyComponent := TEdit.Create(Form1); MyComponent.Left := 10; MyComponent.Top := 10; MyComponent.Parent := Form1; MyComponent.Text := IntToStr(MyList.Count); MyList.Add(MyComponent); end; procedure TForm1.Button1Click(Sender: TObject); var i : integer; begin for i := 0 to MyList.Count - 1 do begin TComponent(MyList.Items[i]).Free; end; end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin MyList.Free; end;我不是高手, 高手是正在銀幕前微笑的人.
------
我不是高手, 高手是正在銀幕前微笑的人. |
fanny
一般會員 發表:10 回覆:26 積分:7 註冊:2002-07-19 發送簡訊給我 |
procedure TRDQuotationForm.LoadBtnClick(Sender: TObject);
var pan,: TPanel;dbg1: TDBGrid;bit1,bit2: TBitBtn;
begin
pan:=TPanel.Create(self);
with pan do
begin
parent :=RDQuotationForm;
Top := 2;
Left := 2;
Height := 460;
Width := 712;
color := clBlack;
Visible:= True; dbg1:=TDBGrid.Create(self);
with dbg1 do
begin
parent := pan;
Align := alTop;
Height := 367;
Fixedcolor := clYellow;
color := clSilver;
DataSource := KKDM.BufferADS;
ReadOnly := False;
Visible := True;
end; bit1:=TBitBtn.Create(self);
with bit1 do
begin
parent := pan;
Height := 28;
top := parent.Height-60;
Width := 120;
left := parent.width-350;
Visible := True;
Caption := '確認轉入';
tag := 1;
OnClick := ButtonClick;
end; bit2:=TBitBtn.Create(self);
with bit2 do
begin
parent := pan;
Height := 28;
top := parent.Height-60;
Width := 120;
left := parent.width-200;
Visible := True;
Caption := '取消返回';
tag := 2;
OnClick := ButtonClick;
end;
end;
end; procedure TRDQuotationForm.ButtonClick(Sender: TObject);
begin
case (Sender as TButton).Tag of
1: begin
end;
2: begin
(Sender as TButton).parent.Free; //bit2
end;
end;
end; //run 到此後就出現message 這是產生元件程式,bit2 on click 後就會產生message
|
fanny
一般會員 發表:10 回覆:26 積分:7 註冊:2002-07-19 發送簡訊給我 |
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
引言:If I understand correctly, this event handler is executed by clicking bit2. You are freeing its parent (pan) and some windows messages will be dispatched to controls displayed inside it. Since freeing of parent control (not owner) does not necessary free the controls displayed on it I would suggest either free each controls manually or chagne their 'Owner' to pan. Here is the way I tested:procedure TRDQuotationForm.ButtonClick(Sender: TObject); begin case (Sender as TButton).Tag of 1: begin end; 2: begin (Sender as TButton).parent.Free; //bit2 end; end; end; //run 到此後就出現message 這是產生元件程式,bit2 on click 後就會產生message procedure TRDQuotationForm.ButtonClick(Sender: TObject); var i: integer; pan: TWinControl; bit2: TButton; begin case (Sender as TButton).Tag of 1: begin end; 2: begin bit2 := (Sender as TButton); pan := bit2.parent; bit2.Parent := nil; for i := pan.ControlCount-1 downto 0 do pan.Controls[i].Free; pan.Free; bit2.free; end; end; |
fanny
一般會員 發表:10 回覆:26 積分:7 註冊:2002-07-19 發送簡訊給我 |
[/quote]
If I understand correctly, this event handler is executed by clicking bit2. You are freeing its parent (pan) and some windows messages will be dispatched to controls displayed inside it. Since freeing of parent control (not owner) does not necessary free the controls displayed on it I would suggest either free each controls manually or chagne their 'Owner' to pan. Here is the way I tested:
procedure TRDQuotationForm.ButtonClick(Sender: TObject); var i: integer; pan: TWinControl; bit2: TButton; begin case (Sender as TButton).Tag of 1: begin end; 2: begin bit2 := (Sender as TButton); pan := bit2.parent; bit2.Parent := nil; for i := pan.ControlCount-1 downto 0 do pan.Controls[i].Free; pan.Free; bit2.free; end; end;把【bit2.free;】那一行攔掉就可以了, 不過再請問一下,那我bit2要如何free掉? Thanks~~ [/quote] |
Miles
尊榮會員 發表:27 回覆:662 積分:622 註冊:2002-07-12 發送簡訊給我 |
Hello :
照william先進的這段程式Bit2已經Free掉了吧
procedure TRDQuotationForm.ButtonClick(Sender: TObject); var i: integer; pan: TWinControl; bit2: TButton; begin case (Sender as TButton).Tag of 1: begin end; 2: begin bit2 := (Sender as TButton); pan := bit2.parent; bit2.Parent := nil; for i := pan.ControlCount-1 downto 0 do pan.Controls[i].Free; pan.Free; end; end;我不是高手, 高手是正在銀幕前微笑的人.
------
我不是高手, 高手是正在銀幕前微笑的人. |
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
|
fanny
一般會員 發表:10 回覆:26 積分:7 註冊:2002-07-19 發送簡訊給我 |
引言:引言: 把【bit2.free;】那一行攔掉就可以了, 不過再請問一下,那我bit2要如何free掉? Thanks~~??? Did you encounter any problem in this line? I have tested the above code and it works fine on my copy of Delphi 7. It must cancel the code of "bit2.free" like Miles's supply code. And now I don't know how to free TBitBtn(bit2) or it's already been free? |
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
I have no idea, btw what error do you encounter? Can I have the code?. Since bit2 is created using the form as owner, it will be freed upon destruction of the form. By setting bit2.Parent to nil, bit2 should be invisible and it is not contained in the controls list of pan and hence it won't get any message when pan is being destroyed.
|
fanny
一般會員 發表:10 回覆:26 積分:7 註冊:2002-07-19 發送簡訊給我 |
引言: I have no idea, btw what error do you encounter? Can I have the code?. Since bit2 is created using the form as owner, it will be freed upon destruction of the form. By setting bit2.Parent to nil, bit2 should be invisible and it is not contained in the controls list of pan and hence it won't get any message when pan is being destroyed.If I didn't delete the code of "bit2.free",it appeared the first time message.Now it worked successfully,but i don't know how to free the "bit2". Is "bit2" be free at the end of program If I never free it? Will it cost space If I run this procedure many times? |
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
引言: If I didn't delete the code of "bit2.free",it appeared the first time message.Now it worked successfully,but i don't know how to free the "bit2". Is "bit2" be free at the end of program If I never free it? Will it cost space If I run this procedure many times?This sounds strange, I have no problem using Delphi 7 and I really cannot find any problem with the code... If you do not free bit2 manually, it would be destroyed upon its OWNER destruction (i.e. the form) and it does use up some resource/memory upon repeated iteration untill the form is destroyed. |
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
|
chih
版主 發表:48 回覆:1186 積分:639 註冊:2002-04-02 發送簡訊給我 |
|
fanny
一般會員 發表:10 回覆:26 積分:7 註冊:2002-07-19 發送簡訊給我 |
|
fanny
一般會員 發表:10 回覆:26 積分:7 註冊:2002-07-19 發送簡訊給我 |
引言: Just a thought, do not typecast using "as" in this case. i.e. (Sender as TButton) change to TButton(Sender). See if this helps. I change the code as below. procedure TRDQuotationForm.ButtonClick(Sender: TObject); var i: integer; pan: TWinControl; bit2: TButton; begin bit2 := (Sender as TButton); pan := bit2.parent; bit2.Parent := nil; for i := pan.ControlCount-1 downto 0 do pan.Controls[i].Free; pan.Free; bit2.free; RDQuotationForm.SetFocus; end; It also show the same message. How can I test bit2 existence? |
chih
版主 發表:48 回覆:1186 積分:639 註冊:2002-04-02 發送簡訊給我 |
|
Miles
尊榮會員 發表:27 回覆:662 積分:622 註冊:2002-07-12 發送簡訊給我 |
Hello fanny:
引言: Just a thought, do not typecast using "as" in this case. i.e. (Sender as TButton) change to TButton(Sender). See if this helps. I change the code as below. procedure TRDQuotationForm.ButtonClick(Sender: TObject); var i: integer; pan: TWinControl; bit2: TButton; begin bit2 := (Sender as TButton); pan := bit2.parent; bit2.Parent := nil; for i := pan.ControlCount-1 downto 0 do pan.Controls[i].Free; pan.Free; //bit2.free; bit2是pan的子項, 所以在pan.Controls[i].Free這個迴圈裡已經將他Free掉了若是後面在加bit2.Free這樣會錯 RDQuotationForm.SetFocus; end; It also show the same message. How can I test bit2 existence?我不是高手, 高手是正在銀幕前微笑的人.
------
我不是高手, 高手是正在銀幕前微笑的人. |
fanny
一般會員 發表:10 回覆:26 積分:7 註冊:2002-07-19 發送簡訊給我 |
引言: Hello fanny:引言: Just a thought, do not typecast using "as" in this case. i.e. (Sender as TButton) change to TButton(Sender). See if this helps. I change the code as below. procedure TRDQuotationForm.ButtonClick(Sender: TObject); var i: integer; pan: TWinControl; bit2: TButton; begin bit2 := (Sender as TButton); pan := bit2.parent; bit2.Parent := nil; for i := pan.ControlCount-1 downto 0 do pan.Controls[i].Free; pan.Free; //bit2.free; bit2是pan的子項, 所以在pan.Controls[i].Free這個迴圈裡已經將他Free掉了若是後面在加bit2.Free這樣會錯 RDQuotationForm.SetFocus; end; It also show the same message. How can I test bit2 existence?可是【bit2.Parent := nil;】這一行指令不是已經切斷了跟pan之間的關係了,若把pan釋放掉,bit2也會影響到? |
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
引言: Just a thought, do not typecast using "as" in this case. i.e. (Sender as TButton) change to TButton(Sender). See if this helps. I change the code as below.Since I cannot reproduce the problem myself, would you mind testing it? 發表人 - william 於 2002/11/13 16:45:47procedure TRDQuotationForm.ButtonClick(Sender: TObject); var i: integer; pan: TWinControl; bit2: TButton; begin // bit2 := (Sender as TButton); bit2 := TButton(Sender); pan := bit2.parent; bit2.Parent := nil; for i := pan.ControlCount-1 downto 0 do pan.Controls[i].Free; pan.Free; bit2.free; RDQuotationForm.SetFocus; end;It also show the same message. How can I test bit2 existence? |
fanny
一般會員 發表:10 回覆:26 積分:7 註冊:2002-07-19 發送簡訊給我 |
引言:引言: Just a thought, do not typecast using "as" in this case. i.e. (Sender as TButton) change to TButton(Sender). See if this helps. I change the code as below.Since I cannot reproduce the problem myself, would you mind testing it? OK~~thanks~~ Can I ask you where the difference? 發表人 - william 於 2002/11/13 16:45:47procedure TRDQuotationForm.ButtonClick(Sender: TObject); var i: integer; pan: TWinControl; bit2: TButton; begin // bit2 := (Sender as TButton); bit2 := TButton(Sender); pan := bit2.parent; bit2.Parent := nil; for i := pan.ControlCount-1 downto 0 do pan.Controls[i].Free; pan.Free; bit2.free; RDQuotationForm.SetFocus; end;It also show the same message. How can I test bit2 existence? |
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
引言: OK~~thanks~~ Can I ask you where the difference?So it works for you? There are 2 ways to do type casting. The first one is directly using the type and the other is using as. I think one is using compile time information and the other is using RTTI (forget which one and whether it is correct or not). IMHO, using "as" only when you are type casting an interface and always use Txxx(xxx) kind of type cast for classes. In this case, I think that the "as" has make an interface and associate with the local variable bit2 (strange and wrong? my copy of Delphi 7 works fine...), so when the variable goes out of scope, it will need to be destroyed (codes generated by the compiler?). Since bit2 is freed already and trying to free it again will cause access violation (at the very end of the procedure since the codes are generated by the compiler). |
fanny
一般會員 發表:10 回覆:26 積分:7 註冊:2002-07-19 發送簡訊給我 |
|
Emulator
一般會員 發表:1 回覆:18 積分:8 註冊:2002-10-17 發送簡訊給我 |
引言:Owner才是負責記憶體.. Parent只是負責重繪..=.= = Delphi - Emulator =引言: Hello fanny:引言: Just a thought, do not typecast using "as" in this case. i.e. (Sender as TButton) change to TButton(Sender). See if this helps. I change the code as below. procedure TRDQuotationForm.ButtonClick(Sender: TObject); var i: integer; pan: TWinControl; bit2: TButton; begin bit2 := (Sender as TButton); pan := bit2.parent; bit2.Parent := nil; for i := pan.ControlCount-1 downto 0 do pan.Controls[i].Free; pan.Free; //bit2.free; bit2是pan的子項, 所以在pan.Controls[i].Free這個迴圈裡已經將他Free掉了若是後面在加bit2.Free這樣會錯 RDQuotationForm.SetFocus; end; It also show the same message. How can I test bit2 existence?可是【bit2.Parent := nil;】這一行指令不是已經切斷了跟pan之間的關係了,若把pan釋放掉,bit2也會影響到?
------
= Delphi - Emulator = |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |