代碼如下,
private void StartForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (DialogResult.OK == MessageBox.Show("確認退出?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
{
Application.Exit();
}
else
e.Cancel = true;
}
而我把Application.Exit()注釋掉就正常彈出一次對話框了,新人不解!請大家幫幫忙!
這個是很正常的,當執行Application.Exit();時,就激活窗體的關閉事件,從而調用該事件的處理程序StartForm_FormClosing,你這樣寫相當於遞歸調用
,修改如下:
private void StartForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (DialogResult.Cancel == MessageBox.Show("確認退出?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
{
e.Cancel = true;
}
}