I am trying to view a message box. this should happen when the user tries to exit the program while there are child windows active. if there are no active child windows then the application should end. i have written some code but when the user clicks on NO, the message box immediately pops up again.if you have a better way of doing this please feel free to share, thank you.
private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
{
Form child = this.ActiveMdiChild;
if(child != null)
{
DialogResult res =
MessageBox.Show( child.Name +
"Do you want to exit?", "Exit",
MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (res == DialogResult.No)
{
e.Cancel = true;
child.Focus();
}
else if (res == DialogResult.Yes)
{
Application.Exit();
}
}
else if (child == null)
{
Application.Exit();
}
}
According to MSDN if there are child form who canceled closing then main form will have its
e.Cancelalready set to true. Also there’s no need to callApplication.Exit(), because after the main form closes the application should end. Try this: