Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When i close the application,i want a confirmation box to popup before closing the form.
I have written the code for it but its not working.
Thanx in advance.
Here is my code for closing of the form:

C#
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
      {
          if (MessageBox.Show("Do you want to save changes to your text?", "My Application",
            MessageBoxButtons.YesNo) == DialogResult.Yes)
          {

              e.Cancel = true;

          }
          objS3Instance = null;
          Environment.Exit(0);



      }
Posted
Comments
syed shanu 20-Aug-13 1:55am    
Check your form Closing Event is Triggered using Break point

1 solution

You must exit the method (or call the Save() method you may have) after setting the e.Cancel = true. Now the application terminates not matter what, because Environment.Exit(0); always gets executed.

C#
if (MessageBox.Show("Do you want to save changes to your text?", "My Application",
  MessageBoxButtons.YesNo) == DialogResult.Yes)
{

    e.Cancel = true;

    // Do some stuff here

    return;
}
objS3Instance = null;
Environment.Exit(0);
 
Share this answer
 

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