unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,Unit3;type
TForm1 = class(TForm)
btn1: TButton;
btn2: TButton;
edt1: TEdit;
edt2: TEdit;
Label1: TLabel;
Label2: TLabel;
procedure btn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
Fman: Tman;
procedure Setman(Value: Tman);
function Getman:Tman;
public
property man: Tman read Getman write Setman;
end;var
Form1: TForm1;implementationuses
Unit2;{$R *.dfm}{
************************************ TForm1 ************************************
}
procedure TForm1.btn1Click(Sender: TObject);
begin
form2:=TForm2.Create(application);
form2.Label1.Caption:=Self.edt1.Text;
form2.Label2.Caption:=self.edt2.Text;
Form2.ShowModal;
end;procedure TForm1.Setman(Value: Tman);
begin
Fman := Value;
end;function tform1.Getman:Tman;
begin
Fman.username:=edt1.Text;
fman.sex:=edt2.Text;
Result:=Fman;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
fman:=tman.Create;
end;procedure TForm1.btn2Click(Sender: TObject);
begin
form2:=TForm2.Create(application);
Form2.man02:=man;
Form2.ShowModal;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
fman.Free;
end;end.
=========================================
unit Unit2;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,Unit3 ;type
TForm2 = class(TForm)
Label1: TLabel;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
Fman02: Tman;
procedure Setman02(Value: Tman);
public
property man02: Tman read Fman02 write Setman02;
end;var
Form2: TForm2;implementation{
************************************ TForm2 ************************************
}
procedure TForm2.Setman02(Value: Tman);
begin
Fman02 := Value;
Label1.Caption:=Fman02.username;
Label2.Caption:=Fman02.sex;
end;{$R *.dfm}procedure TForm2.FormCreate(Sender: TObject);
begin
fman02:=tman.Create;
end;procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
fman02.Free;
end;end.==============================================
unit Unit3;interfacetype
Tman = class(TObject)
private
Fsex: string;
Fusername: string;
public
property sex: string read Fsex write Fsex;
property username: string read Fusername write Fusername;
end;implementation
end.
以上为本程序的三个单元
在unit1里点击btn2通过类调用显示窗体2,然后关闭窗体2,然后关闭窗体1时出错。附件为本程序。
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,Unit3;type
TForm1 = class(TForm)
btn1: TButton;
btn2: TButton;
edt1: TEdit;
edt2: TEdit;
Label1: TLabel;
Label2: TLabel;
procedure btn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
Fman: Tman;
procedure Setman(Value: Tman);
function Getman:Tman;
public
property man: Tman read Getman write Setman;
end;var
Form1: TForm1;implementationuses
Unit2;{$R *.dfm}{
************************************ TForm1 ************************************
}
procedure TForm1.btn1Click(Sender: TObject);
begin
form2:=TForm2.Create(application);
form2.Label1.Caption:=Self.edt1.Text;
form2.Label2.Caption:=self.edt2.Text;
Form2.ShowModal;
end;procedure TForm1.Setman(Value: Tman);
begin
Fman := Value;
end;function tform1.Getman:Tman;
begin
Fman.username:=edt1.Text;
fman.sex:=edt2.Text;
Result:=Fman;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
fman:=tman.Create;
end;procedure TForm1.btn2Click(Sender: TObject);
begin
form2:=TForm2.Create(application);
Form2.man02:=man;
Form2.ShowModal;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
fman.Free;
end;end.
=========================================
unit Unit2;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,Unit3 ;type
TForm2 = class(TForm)
Label1: TLabel;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
Fman02: Tman;
procedure Setman02(Value: Tman);
public
property man02: Tman read Fman02 write Setman02;
end;var
Form2: TForm2;implementation{
************************************ TForm2 ************************************
}
procedure TForm2.Setman02(Value: Tman);
begin
Fman02 := Value;
Label1.Caption:=Fman02.username;
Label2.Caption:=Fman02.sex;
end;{$R *.dfm}procedure TForm2.FormCreate(Sender: TObject);
begin
fman02:=tman.Create;
end;procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
fman02.Free;
end;end.==============================================
unit Unit3;interfacetype
Tman = class(TObject)
private
Fsex: string;
Fusername: string;
public
property sex: string read Fsex write Fsex;
property username: string read Fusername write Fusername;
end;implementation
end.
以上为本程序的三个单元
在unit1里点击btn2通过类调用显示窗体2,然后关闭窗体2,然后关闭窗体1时出错。附件为本程序。
这句改为 form2:=TForm2.Create(self); 试试
---------------------------
Debugger Exception Notification
---------------------------
Project Project1.exe raised exception class EAccessViolation with message 'Access violation at address 00000000. Read of address 00000000'. Process stopped. Use Step or Run to continue.
---------------------------
OK Help
---------------------------
也就是说,把
procedure TForm2.FormCreate(Sender: TObject);
begin
fman02:=tman.Create;
end; procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
fman02.Free;
end;
这两段删掉。
不要这句话就没事。
程序下载地址:
http://www.chinaztt.com/mail/sample.rar
TForm1中,把man赋值给了Form2的man02
begin
form2:=TForm2.Create(application);
Form2.man02:=man;
Form2.ShowModal;
而在form2中,关闭的时候已经释放了man02
所以在form1退出的时候 man.free会异常,2个free删掉任何一个都可以。
form2中的tman.Create不需要。
另外:Corn1 ,gzmhero 你们delphi建模吗,用什么工具,我现在学modelmakertools.
能给点建议吗?
form2中的tman.Create为什么不需要创建,不创建怎么可以赋值呢?
property man: Tman read Getman write Setman;
属性都是指针类型吗?
类,可以看作以指针的形式存放。