P
pengcq2008
Unregistered / Unconfirmed
GUEST, unregistred user!
有一个主窗体form1 有两个被调用的窗体form2,form3 , form1可以调用form2,也可以调用form3,form2与form3可以相互多次调用,但是最后一个被调用出来的窗体退出后要直接回到主窗体form1,比如Form2最后一次调用了Form3,则Form3退出后直接回到主窗体Form1, 我做了一个简单事例程序,这个程序不会报错,但是Form2,Form3被调用一次,就创建一次,调用多个次就会出多个窗口,很占用资源,这样要关闭多次才能关闭完Form2,Form3.(有个方法就是要判断被调用的窗体是否存在,如果不存在则创建该窗体,否则直接显示出来.这个方法可以考虑),下面是例程的代码: 谢谢大家帮忙!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses unit2,unit3 ;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if not Assigned(Form2) then
begin
Form2:=TForm2.Create(nil);
try
Form2.ShowModal;
finally
FreeAndNil(Form2);
end;
end
else
Form2.ShowModal;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if not Assigned(Form3) then
begin
Form3:=TForm3.Create(nil);
try
Form3.ShowModal;
finally
FreeAndNil(Form3);
end;
end
else
Form3.ShowModal;
end;
end.
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
uses unit3;
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
begin
// if Assigned(Form3) then
// FreeAndNil(Form3);
// if not Assigned(Form3) then
begin
Form3:=TForm3.Create(nil);
try
Form3.ShowModal;
finally
FreeAndNil(Form3);
end;
end ;
{ else
begin
FreeAndNil(Form3);
Form3.ShowModal;
end;
}
end;
procedure TForm2.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Action:=caFree;
end;
procedure TForm2.FormDestroy(Sender: TObject);
begin
Form3:=nil;
end;
end.
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm3 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
uses unit2;
{$R *.dfm}
procedure TForm3.Button1Click(Sender: TObject);
begin
// if Assigned(Form2) then
// FreeAndNil(Form2);
// if not Assigned(Form2) then
begin
Form2:=TForm2.Create(nil);
try
Form2.ShowModal;
finally
FreeAndNil(Form2);
end;
// end
{ else
begin
FreeAndNil(Form2);
Form2.ShowModal;
}
end;
end;
procedure TForm3.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Action:=caFree;
end;
procedure TForm3.FormDestroy(Sender: TObject);
begin
Form3:=nil;
end;
end.
program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {Form2},
Unit3 in 'Unit3.pas' {Form3};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses unit2,unit3 ;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if not Assigned(Form2) then
begin
Form2:=TForm2.Create(nil);
try
Form2.ShowModal;
finally
FreeAndNil(Form2);
end;
end
else
Form2.ShowModal;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if not Assigned(Form3) then
begin
Form3:=TForm3.Create(nil);
try
Form3.ShowModal;
finally
FreeAndNil(Form3);
end;
end
else
Form3.ShowModal;
end;
end.
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
uses unit3;
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
begin
// if Assigned(Form3) then
// FreeAndNil(Form3);
// if not Assigned(Form3) then
begin
Form3:=TForm3.Create(nil);
try
Form3.ShowModal;
finally
FreeAndNil(Form3);
end;
end ;
{ else
begin
FreeAndNil(Form3);
Form3.ShowModal;
end;
}
end;
procedure TForm2.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Action:=caFree;
end;
procedure TForm2.FormDestroy(Sender: TObject);
begin
Form3:=nil;
end;
end.
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm3 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
uses unit2;
{$R *.dfm}
procedure TForm3.Button1Click(Sender: TObject);
begin
// if Assigned(Form2) then
// FreeAndNil(Form2);
// if not Assigned(Form2) then
begin
Form2:=TForm2.Create(nil);
try
Form2.ShowModal;
finally
FreeAndNil(Form2);
end;
// end
{ else
begin
FreeAndNil(Form2);
Form2.ShowModal;
}
end;
end;
procedure TForm3.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Action:=caFree;
end;
procedure TForm3.FormDestroy(Sender: TObject);
begin
Form3:=nil;
end;
end.
program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {Form2},
Unit3 in 'Unit3.pas' {Form3};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.