I am using following code on closing event of my Windows forms application form:
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dr = MessageBox.Show("Are you sure you want to quit?", "Leaving App",MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.No)
{
return;
}
else
Application.Exit();
}
But the problem is, whenever user clicks Yes the application ends but when user clicks No, the application is still running but the form hides. How can I keep form visible when user clicks No?
Update
Another problem I am facing is that, when I click Yes, the MessageBox displays again and then I click Yes the application exits. Why its displaying for two times?
You don't have to call Application.Exit()
because if you do not cancel closing it program will exit itself. Below is corrected code that is working:
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dr = MessageBox.Show("Are you sure you want to quit?", "Leaving App",MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.No)
{
e.Cancel = true;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With