我有一个TAction.OnExecute
从我的主窗体触发的事件处理程序
FormPrincipal
,它会打开其他窗体。
procedure TFormPrincipal.AbreFormBancoExecute(Sender: TObject);
begin
Formbanco := Tformbanco.Create(self);
Formbanco.Parent := PanelCorpo;
Formbanco.Align := alclient;
Formbanco.BorderIcons := [];
Formbanco.Show;
Formbanco.BorderStyle := bsNone;
Formbanco.SetFocus;
end;
一旦我有几个表格,在触发OnExecute
打开另一个表格之前如何知道打开了哪个表格以及如何关闭它?
=========== 终于按我的预期工作了======= 主要形式是form1,我从中调用form2和form3。在 form1 中,我有一个 panel1,它是 form2 和 form3 的父级。见 form1 代码: ... var Form1: TForm1;
implementation
{$R *.dfm}
uses unit2, unit3;
procedure Tform1.CloseActiveForm (Formname : string);
// Free memory allocated to the current form , set it to nil
// I'll have to find a better way to perform FreeanNil without
// use many IFs command
begin
if Formname = 'form2' then FreeAndnil(Form2) else
if Formname = 'form3' then FreeandNil(Form3);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
CloseActiveForm(Edit1.Text); //Edit1 has the current active form name
if form2 = nil then
begin
Application.CreateForm(Tform2,Form2);
Form2.Parent := Panel1;
Form2.Align := alclient;
Form2.Show;
Form2.BorderStyle := bsnone;
Form2.SetFocus;
Form2.OnActivate(Sender); //Method Show blocks Activate event
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
CloseActiveForm(Edit1.Text); //Edit1 has the current active form name
if form3 = nil then
begin
Application.CreateForm(Tform3,Form3);
Form3.Parent := Panel1;
Form3.Align := alclient;
Form3.Show;
Form3.BorderStyle := bsnone;
Form3.SetFocus;
Form3.OnActivate(Sender); //Method Show blocks Activate event
end;
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
Edit1.Text := Screen.ActiveForm.Name;
end;
end.
2)form2和form3中的代码(认为form3是相同的)
...
var
Form2: TForm2;
implementation
{$R *.dfm}
uses unit1;
procedure TForm2.Button1Click(Sender: TObject);
begin
Edit2.Text := Screen.ActiveForm.Name;
end;
procedure TForm2.FormActivate(Sender: TObject);
begin
setfocus;
Edit1.Text := Form2.Name;
Form1.Edit1.Text := Form2.Name; //set form name
// the property Screen.ActiveForm.Name if used here, will always presents
// form1 name (main form) because this form2 is set to a parent panel
// in form1
end;
end.