Click here to Skip to main content
15,896,368 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void Parent_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dr;
            dr = MessageBox.Show("Are you sure you want to exit?", "Confirm Exit", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                Application.Exit();
            }
            else
                e.Cancel = true;
        }

My problem is that when I clicked OK, the messagebox will again pop up asking if I'm going to exit the application.
I want it so that when I click OK, the application will close automatically.

Please help.
Posted
Updated 27-Oct-10 22:37pm
v3
Comments
Dalek Dave 28-Oct-10 4:38am    
Edited for Grammar and Readability.

1 solution

Hi, try this:

C#
private void Parent_FormClosing(object sender, FormClosingEventArgs e)
{
  DialogResult dr;
  dr = MessageBox.Show("Are you sure you want to exit?", "Confirm Exit", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
  if (dr == DialogResult.Cancel)
    e.Cancel = true;
}
 
Share this answer
 
Comments
Pawan Kiran 28-Oct-10 1:43am    
Hi JF, Good Call.
nyzguy25 29-Oct-10 0:18am    
the messagebox will terminate but my program don't.....
nyzguy25 29-Oct-10 0:21am    
tnx for d response....
Sergey Alexandrovich Kryukov 8-Jun-11 21:07pm    
Correct, my 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900