(一)参考文献:C# 添加FormClosing事件
(二)在 项目名.Designer.cs中的InitializeComponent()添加:
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.frmMain_FormClosing);
(三)在 项目名.cs中添加:
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("你确定要退出?数据可能会丢失!", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
e.Cancel = false; //关闭窗体
}
else
e.Cancel = true; //返回窗体
}