如果用户想通过单击退出图标或按 ALT+F4 退出应用程序,我想创建一个对话框,询问用户是否确实确定要退出。
如何在应用程序实际关闭之前捕获此事件?
查看关闭时 http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onclosing.aspx表单的事件。
以下是该链接的摘录,实际检查文本字段上的更改并提示保存:
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Determine if text has changed in the textbox by comparing to original text.
if (textBox1.Text != strMyOriginalText)
{
// Display a MsgBox asking the user to save changes or abort.
if(MessageBox.Show("Do you want to save changes to your text?", "My Application",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// Cancel the Closing event from closing the form.
e.Cancel = true;
// Call method to save file...
}
}
}
您可以更改文本以满足您的需要,然后我认为您可能想要切换DialogResult.Yes
to DialogResult.No
根据您的文字。
这是一些专门为您修改的代码:
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if(MessageBox.Show("Are you sure you want to quit?", "My Application", MessageBoxButtons.YesNo) == DialogResult.No)
{
e.Cancel = true;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)