Delphi中窗体的调用问题。用Show不用showmodal。

2025-05-13 08:40:00
推荐回答(2个)
回答1:

后创建的Form3中的FSerial为影响之前创建的Form3的关闭。
这句话不正确。
因为每次创建的对象不同,FSerial属于不同的对象,所以相互之间不会影响。
至于你的问题,出在Repeat ... Until ... 循环上。
窗体多了,CPU对各个TForm3实例中的循环语句,在Application.HandleMessage没有响应,因此循环就检测不到FSerial的值了。
如果你为了释放窗体而设计此循环,可以用一个笨办法来处理:
procedure ShowForm3(ASelf: TForm);
begin
with TForm3.Create(ASelf) do
begin
Parent := ASelf;
Top := 0;
Left := 0;
Caption:=IntToStr(Handle)+' '+IntToStr(Integer(@FSerial)) +' '+IntToStr(Serial);
Show;
FSerial := 0;
end;
end;

在Form2中增加一个Timer,定时检测FSerian值,从而实现释放的目的:

procedure TForm2.ProcClose;
var
i:Integer;
begin
for i:=0 to ComponentCount-1 do
begin
if Components[i] is TForm3 then
begin
if (Components[i] as TForm3).Serial>0 then
begin
(Components[i] as TForm3).Close;
(Components[i] as TForm3).Free;
Break;///因为删除(释放)掉一个,所以,需要重新开始循环
end;
end;
end;
end;

procedure TForm2.Timer1Timer(Sender: TObject);
begin
ProcClose;
end;

回答2:

这有点像欧几数学,训练人脑袋的,但在编辑实践中,可能一生都用不上。