发现异常如何退出?(50分)

R

rich6

Unregistered / Unconfirmed
GUEST, unregistred user!
[:(!]在Form1中有一按钮,当按button的时候,弹出一个另外的Form2。
Form2在执行过程中,如果发现某个条件不满足,则自己自动关闭,为什么
在OnCreate和OnActive的事件中,Close方法都不能实现关闭功能?
 
application.Terminate ;
 
application.Terminate 不行,那样会导致Form1也关闭。
 
如果使用的是showmodal,可以使用窗口的返回信息,如下:
form1.tbutton1onclick(sender:tobject);
begin
form2:=tform2.create(self);
form2.showmodal;
if form2.modalresult=mrok then
form2.free
else
form2.free;
end;
然后在form2需要返回的信息中加上: form2.modalresult:=mrok
就可以关闭了(实际上是free了)
 
当然不行了,你应该要注意这段代码:
Application.CreateForm(TForm1, Form1);
Application.Run;
我同意monkeyboys的做法.
 

form2.modalresult:=mrok
系统执行到这条语句会出现异常。
 
你的窗口不是以showmodal显示的,所以异常
 
在form2的事件里面,写上
try
...
except
modalresult := mrCancel //这里就可以让窗口退出
end;
 

form2的except里
PostMessage(handle,WM_CLOSE,0,0)
 
多人接受答案了。
 
顶部