子窗体的关闭问题(30分)

A

AYing

Unregistered / Unconfirmed
GUEST, unregistred user!
我在子窗体(子窗体没有自动创建)放了一个按纽并写代码:subFORM.FREE,在子窗体的
ONCLOSE事件中写代码:subFORM.FREE。工程运行后,第一次按按纽可以正常关闭子窗体
,但再次运行子窗体后,再按按纽就出错了,富翁们帮帮吧?
 
不要在ONCLOSE事件用free,在创建的地方free
 
OnClose事件:
procedure TFrmAbstract.FormClose(Sender: TObject; var Action: TCloseAction);
//关闭窗口时自动释放资源
begin
Action := caFree;
end;
 
OnClose本身就有一个free操作
 
原因在于 当你 form2.close 时应用程序并没有释放邮Application.CreateForm(TForm2, Form2);
创建的form2 ,form2要想free 也由 Application 来管理 否则就会报内存地址读错误
如果你要想要form2 free掉 就不能由Application.CreateForm(TForm2, Form2);
创建form2
 
你可以这么写
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses Unit2;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
form2 :=TForm2.Create(self);
form2.Show;
end;

end.
/////////////
unit Unit2;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;

type
TForm2 = class(TForm)
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;

end.
///////////////
program Project1;

uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {Form2};

{$R *.res}

begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);

Application.Run;
end.
 
TO:青铜三代
我的代码跟你的是一样,但是我是在form2上有一个工具栏按纽(退出),我的意思是按这个
按纽时,窗体free,启动工程后,第一次按这个按纽时不会出错,但再次启动子窗体,再按
这个按纽时,DELPHI就提示出错了。代码如下:
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
procedure TForm2.ToolButton1Click(Sender: TObject);
begin
form2.free;
end;
 
问题我已解决。
在按纽的click事件中的代码为:
form1.close
在窗体的onclose事件中的代码为:
action:=cafree
 
多人接受答案了。
 

Similar threads

回复
0
查看
919
不得闲
S
回复
0
查看
649
SUNSTONE的Delphi笔记
S
S
回复
0
查看
729
SUNSTONE的Delphi笔记
S
顶部