如何防止或阻止关闭WinForms窗口? [英] How to prevent or block closing a WinForms window?
本文介绍了如何防止或阻止关闭WinForms窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何通过显示 MessageBox
来防止窗口关闭? (技术: WinForms
和 C#
)
How can I prevent window closing by showing a MessageBox
? (Technology:WinForms
with C#
)
当发生关闭事件,我希望运行以下代码:
When the close event occurs I want the following code to be run:
private void addFile_FormClosing( object sender, FormClosingEventArgs e ) {
var closeMsg = MessageBox.Show( "Do you really want to close?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question );
if (closeMsg == DialogResult.Yes) {
//close addFile form
} else {
//ignore closing event
}
}
推荐答案
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
var window = MessageBox.Show(
"Close the window?",
"Are you sure?",
MessageBoxButtons.YesNo);
e.Cancel = (window == DialogResult.No);
}
这篇关于如何防止或阻止关闭WinForms窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文