Winform窗体关闭时判断是否关闭

在窗体的关闭事件FormClosing中进行判断,FormClosing事件每当用户关闭窗体时,在窗体已关闭并指定关闭原因前发生

 1         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
 2         {
 3             DialogResult r = MessageBox.Show("是否关闭窗体?", "提示", MessageBoxButtons.YesNo);
 4             if (r == DialogResult.Yes)//点击“是”按钮
 5             {
 6                 e.Cancel = false;//事件的取消为假,关闭窗体
 7             }
 8             else
 9             {
10                 e.Cancel = true;//事件的取消为真,不关闭窗体
11             }
12         }

 

posted @ 2016-09-27 20:38  西枫叶落  阅读(4566)  评论(0编辑  收藏  举报