我想回到form1但是每次单击后退按钮时,它都会触发FormClosing事件,因此它会提示我是否要退出应用程序。如何从FormClosing事件中排除其他按钮来解决此问题?
private void Form2_FormClosing_1(object sender, FormClosingEventArgs e)
{
DialogResult dialog = MessageBox.Show("Do you really want to quit?", "Exit",
MessageBoxButtons.YesNo);
if (dialog == DialogResult.Yes)
{
Application.Exit();
}
else if (dialog == DialogResult.No)
{
e.Cancel = true;
}
}
private void back_button_Click(object sender, EventArgs e)
{
this.Hide();
}
答案 0 :(得分:0)
最好的办法是设置全局变量,告诉关闭事件显示/不显示该对话框。
尝试这些方法。
private bool CalledFromButton = false;
private void Form2_FormClosing_1(object sender, FormClosingEventArgs e)
{
if(CalledFromButton)
return;
DialogResult dialog = MessageBox.Show("Do you really want to quit?", "Exit",
MessageBoxButtons.YesNo);
if (dialog == DialogResult.Yes)
{
Application.Exit();
}
else if (dialog == DialogResult.No)
{
e.Cancel = true;
}
}
private void back_button_Click(object sender, EventArgs e)
{
CalledFromButton = true;
this.Hide();
}