【发布时间】:2016-11-16 11:25:07
【问题描述】:
在我的主表单中,我有一个按钮可以打开一个模态 Form2(它可能会打开其他模态表单)。在打开 Form2 之前,我正在设置一个计时器,它将以编程方式关闭所有活动的模态表单 (Form2.Close) 并打开一个新的模态 Form3。
问题是当Form3 以模态方式打开时,Form2 仍然存在(可见),并且只有当我通过单击X 关闭Form3 时Form2 才会关闭。
要重现添加 3 个表单到项目添加 TButton,并在 Form1(主表单)上删除 TTimer:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
uses Unit2, Unit3;
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
Timer1.Enabled := False;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Timer1.Enabled := True;
with TForm2.Create(Application) do
try
ShowModal;
finally
Free;
end;
end;
procedure CloseActiveModalForms;
var
I: Integer;
F: TCustomForm;
L: TList; // list of modal forms
begin
L := TList.Create;
try
for I := 0 to Screen.CustomFormCount - 1 do
begin
F := Screen.CustomForms[I];
if (fsModal in F.FormState) then
L.Add(F);
end;
for I := 0 to L.Count - 1 do
TCustomForm(L.Items[I]).Close; // this sets ModalResult := mrCancel
finally
L.Free;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Timer1.Enabled := False;
CloseActiveModalForms; // this should close TForm2 but it does not.
with TForm3.Create(Application) do // create new Modal TForm3
try
ShowModal;
finally
Free;
end;
end;
end.
为什么Form2 没有关闭?为什么我调用CloseActiveModalForms 后Form2 模态循环没有退出?
【问题讨论】:
标签: delphi