表单关闭事件在Windows应用程序中不起作用。 [英] Form closing event not working in windows application.
本文介绍了表单关闭事件在Windows应用程序中不起作用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我关闭应用程序时,我想要在关闭表单之前弹出一个确认框。
我已经为它编写了代码,但是它无效。
Thanx提前。
这是关闭表单的代码:
private void frmMain_FormClosing( object sender,FormClosingEventArgs e)
{
if (MessageBox.Show( 是否要保存对文本的更改?, 我的应用程序,
MessageBoxButtons.YesNo)== DialogResult.Yes)
{
e.Cancel = true ;
}
objS3Instance = null ;
Environment.Exit( 0 );
}
解决方案
您必须退出方法(或致电设置e.Cancel = true
后,可能有Save()方法。现在应用程序终止并不重要,因为Environment.Exit(0);
总是被执行。
< pre lang =cs> if (MessageBox.Show( 是否要保存对文本的更改?, 我的应用程序 ,
MessageBoxButtons.YesNo)== DialogResult.Yes)
{
e.Cancel = true ;
// 在这里做一些事情
返回;
}
objS3Instance = null ;
Environment.Exit( 0 );
When i close the application,i want a confirmation box to popup before closing the form.
I have written the code for it but its not working.
Thanx in advance.
Here is my code for closing of the form:
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Do you want to save changes to your text?", "My Application",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
e.Cancel = true;
}
objS3Instance = null;
Environment.Exit(0);
}
解决方案
You must exit the method (or call the Save() method you may have) after setting thee.Cancel = true
. Now the application terminates not matter what, becauseEnvironment.Exit(0);
always gets executed.
if (MessageBox.Show("Do you want to save changes to your text?", "My Application", MessageBoxButtons.YesNo) == DialogResult.Yes) { e.Cancel = true; // Do some stuff here return; } objS3Instance = null; Environment.Exit(0);
这篇关于表单关闭事件在Windows应用程序中不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文