动态创建的Form的问题。(50分)

  • 主题发起人 主题发起人 longerhe
  • 开始时间 开始时间
L

longerhe

Unregistered / Unconfirmed
GUEST, unregistred user!
frm: array[1..10] of TForm2;
这个在form2中的public中申明。
procedure TForm1.BtnCreateFormClick(Sender: TObject);
var
i:integer;
begin
for i:=1 to 10do
begin
if form2.frm=nil then
begin
Application.CreateForm(TForm2, form2.frm);
form2.frm.Caption:=inttostr(i);
form2.frm.ShowModal;
break;
end;
end;
end;

procedure TForm2.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
freeandnil(f[strtoint(caption)]);
Action:=cafree;
end;

我的意思是想创建多个跟form2一样的窗体。这样的写frm只能被创建一次。 请问在释放frmp的时候,我如何能再次创建frm,i代表某个被创建的窗体。
 
//工程单元
program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {Form2};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
// Application.CreateForm(TForm2, Form2);
Application.Run;
end.

//窗体单元
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
uses
Unit2;
var
frm: array[1..10] of TForm2;
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
begin
for i := 1 to 10do
begin
if frm = nil then
begin
Application.CreateForm(TForm2,frm);
frm.Caption := IntToStr(i);
frm.Show;
end;
end;
end;

procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
var
i: Integer;
begin
for i:= 1 to 10do
frm.Free;
end;

end.
 
你沒有明白我的意思吧,我是要在frm關閉的時候釋放。你在TForm1.FormClose當然能釋放資源,因為整個project1都被close了。
你在form1再放一個button2寫你釋放frm看看,釋放後不能在次create.
procedure TForm1.Button2Click(Sender: TObject);
var
i: Integer;
begin
for i:= 1 to 10do
frm.Free;
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.CreateForm(TForm2, Form2);
Application.Run;
end.

//单元 Unit1
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
procedure SetFormNil(const AIndex: Integer);
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
uses
Unit2;
var
frm: array[1..2] of TForm2;
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
begin
for i := 1 to 2do
begin
if frm = nil then
begin
Application.CreateForm(TForm2,frm);
frm.Caption := IntToStr(i);
frm.FormIndex := i;
frm.Show;
end;
end;
end;

procedure TForm1.SetFormNil(const AIndex: Integer);
begin
frm[AIndex] := nil;
end;

end.

//单元 Unit2
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
FFormIndex: Integer;
procedure SetFormIndex(const Value: Integer);
{ Private declarations }
public
property FormIndex: Integer read FFormIndex write SetFormIndex;
{ Public declarations }
end;

var
Form2: TForm2;
implementation
{$R *.dfm}
uses
Unit1;
procedure TForm2.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Action := caFree;
Form1.SetFormNil(FFormIndex);
end;

procedure TForm2.SetFormIndex(const Value: Integer);
begin
FFormIndex := Value;
end;

end.
 
后退
顶部