如何在C#中点击右上方的 “X”即可关闭程序所有进程?
想通过点击 “X” 结束进程似乎没有想象中简单。
此时两步走:
首先加入代码:
//注释代码用于定位
/*private void button1_TextChanged(object sender, EventArgs e)
{
}
*/
private void Form1_FormClosing_1(object sender, FormClosingEventArgs e)
{
DialogResult result;
result = MessageBox.Show("Confirm Exit?", "Tips", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
e.Cancel = false;
System.Environment.Exit(0);
}
else
{
e.Cancel = true;
}
}
其次回到界面本身,找到 “属性” -> "事件“:
在 “行为” 找到 “FormClosing” ,并选择上述代码块中的表单关闭名称:
此时关闭程序就会弹出我们所设置的弹窗:
此时选择 “是” 则可关闭程序。