当用户点击x图标时,我有一个Messagebox来确认退出并且它有效,但我只需要点击按钮Yes几次直到它关闭。任何人都知道为什么或如何解决它?
private void Interfaz_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult salir = MessageBox.Show("¿Seguro que desea cerrar el programa?", "Advertencia", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (salir == DialogResult.Yes)
{
Application.Exit();
}
else
{
e.Cancel = true;
}
答案 0 :(得分:3)
试试不。您已经关闭表单,无需调用Application.Exit,它将触发对FormClosing的第二次调用
private void Interfaz_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult salir = MessageBox.Show("¿Seguro que desea cerrar el programa?", "Advertencia", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (salir == DialogResult.No)
e.Cancel = true;
}
但是,我建议为ClosingReason添加一个测试。
private void Interfaz_FormClosing(object sender, FormClosingEventArgs e)
{
If(e.CloseReason == CloseReason.UserClosing)
{
DialogResult salir = MessageBox.Show("¿Seguro que desea cerrar el programa?",
"Advertencia", MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (salir == DialogResult.No)
e.Cancel = true;
}
}