动态创建并显示窗口的问题?(50分)

  • 主题发起人 主题发起人 11830
  • 开始时间 开始时间
1

11830

Unregistered / Unconfirmed
GUEST, unregistred user!
代码:
我的程序中有5个窗口分别为form1、form2、form3、form4、form5
其中form1为主窗体,其他窗体在使用时动态创建,使用下面的语句已没有问题:
try
  form1:=tform1.Create(Application);
  form1.ShowModal;
finally
  form1.Free;
end;

try
  form2:=tform2.Create(Application);
  form2.ShowModal;
finally
  form2.Free;
end;

try
  form3:=tform3.Create(Application);
  form3.ShowModal;
finally
  form3.Free;
end;

try
  form4:=tform1.Create(Application);
  form4.ShowModal;
finally
  form4.Free;
end;

try
  form5:=tform5.Create(Application);
  form5.ShowModal;
finally
  form5.Free;
end;

我现在想使用一个函数来动态创建并显示窗口,请问这个函数应该怎么写?
我试过
function SohwMyForm(Sender: TObject):string;
begin
TForm(Sender):=tform.Create(Application);
TForm(Sender).ShowModal;
end;
能显示一个空窗体,不是我原来设计好的窗体。请大侠告诉我该怎么写。我先谢谢了。
 
写错了,上面的form1不是动态创建的,是在form1中有4个按键,
每个按键可创建并显示一个窗体。
 
procedure TForm1.CloseChildForm(); //用来关闭其他子窗体,如果不关闭,则不须调用;
var
I:Integer;
begin
I:=Form1.MDIChildCount;
if I>2 then
begin
Form1.MDIChildren[1].Release;
end;
end;

procedure TForm1.ToolButton1Click(Sender: TObject);
begin
Application.CreateForm(TForm3,Form3);
CloseChildForm();
end;

procedure TForm1.ToolButton3Click(Sender: TObject);
begin
Application.CreateForm(TForm4,Form4);
CloseChildForm();
end;

procedure TForm1.ToolButton5Click(Sender: TObject);
begin
Application.CreateForm(TForm5,Form5);
CloseChildForm();
end;

procedure TForm1.ToolButton7Click(Sender: TObject);
begin
Application.CreateForm(TForm6,Form6);
CloseChildForm();
end;

procedure TForm1.ToolButton9Click(Sender: TObject);
begin
Application.CreateForm(TForm7,Form7);
CloseChildForm();
end;

procedure TForm1.ToolButton11Click(Sender: TObject);
begin
Application.CreateForm(TForm8,Form8);
CloseChildForm();
end;

procedure TForm1.ToolButton13Click(Sender: TObject);
begin
Application.CreateForm(TForm9,Form9);
CloseChildForm();
end;
给点分啊!
 
注意要在project菜单->project options->Forms页,将子窗体设为Available forms.
 
下面是函数,调用时传入想出现的窗体.
function ShowMyForm(Form:TForm):Boolean;
begin
Form:=TForm.Create(Application);
with Form do
try
ShowModal;
finally
Free;
end;
end;
 
输入参数为Form类
procedure ShowForm(FormClass: TFormClass);
begin
With FormClass.Create(Self) do
try
ShowModal;
finally
Free;
end;
end;
 
我已自己解决,谢谢大家!
 
后退
顶部