Click here to Skip to main content
15,891,763 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What I'm trying to achieve is that when the user clicks on the red exit button, a prompt will appear first, making sure that the user really wants to exit the program and did not just accidentally click on the exit button.

I have this code:

C#
private void FormA_FormClosing(object sender, FormClosingEventArgs e)
        {


            DialogResult closing = MessageBox.Show("Are you sure you want to exit the game?", "EXIT", MessageBoxButtons.YesNo);


            if (closing == DialogResult.Yes)
            {
                Application.Exit();
            }



        }


And the prompt appears when the user clicks on the Exit button, but even if you click on the No button, it closes the program!
And when you click on Yes, the prompt appears for the second time and you have to click on Yes again to exit the application.

This isn't on the main form by the way.

What am I doing wrong?
Posted
Updated 30-Aug-14 3:58am
v2

You have to do it like this:
C#
private void FormA_FormClosing(object sender, FormClosingEventArgs e)
{
    DialogResult closing = MessageBox.Show("Are you sure you want to exit the game?", "EXIT", MessageBoxButtons.YesNo);
   if (closing == DialogResult.No)
   {
       e.Cancel = true; // if you don't want to exit the game (if you pressed No), cancel the closing   
   }
}

Your code didn't work because when you clicked No, nothing would cancel the closing.

[Edit]

If you want that the application exits when your form (which is not the main form) closes, then you have to exit the application after the child form is closed:
C#
// when creating child form
ChildForm childFrm = new ChildForm();
childFrm.FormClosed += childFrm_FormClosed;

// in childFrm_FormClosed method:
private void childFrm_FormClosed(Object sender, FormClosedEventArgs e)
{
    Application.Exit(); // exit application
}

Note that you have to add the childFrm_FormClosed in the class of the form that creates your child form. You also don't have to do this for every child form, only this one.
 
Share this answer
 
v3
Comments
kmllev 30-Aug-14 10:31am    
Yeah sorry I tried it again and it's working correctly now, thank you so much!
Thomas Daniels 30-Aug-14 10:35am    
You're welcome!
kmllev 30-Aug-14 10:36am    
Oh wait wait, I ran it several times and just noticed that the application doesn't exit when you click on Yes? It closes yes, but it doesn't exit.
Thomas Daniels 30-Aug-14 10:40am    
I updated my answer.
kbrandwijk 30-Aug-14 10:55am    
Your answers makes quite a few assumptions. All child forms should be instantiated from the main form (which isn't always true). You have the same event handler for all child forms (maybe different child forms need different confirmation messages, or no confirmation at all). Closing any child form will always close the main form (maybe that only needs to happen for one specific child form). I think keeping the logic as close as possible to where it's actually needed (in this case, one specific child form) is best. I have done so in my proposed solution.
You need to set
C#
e.Cancel = true
to stop the form from closing, so the code becomes:

C#
private void FormA_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason != CloseReason.ApplicationExitCall)
    {
      DialogResult closing = MessageBox.Show("Are you sure you want to exit the game?", "EXIT", MessageBoxButtons.YesNo);

      if (closing == DialogResult.Yes)
      {
          Application.Exit();
      }
      else
      {
          e.Cancel = true;
      }
    }
}


Technically, you don't need the else, because the Application.Exit call in the If branch will stop execution anyway, but I always try to go for readability as well.
 
Share this answer
 
v4
Comments
kmllev 30-Aug-14 10:08am    
Thank you! It solved the issue with the No button not functioning, but I'm stuck with the prompt appearing AGAIN when you click on Yes, so if you really want to exit, you have to click Yes twice.
kbrandwijk 30-Aug-14 10:14am    
I can imagine you might have that problem when using Application.Exit, because Application.Exit would trigger the FormClosing event that you were already in. Do you also have that problem using the solution form @ProgramFOX?
kmllev 30-Aug-14 10:31am    
Thank you for your help, I've fixed it now. I just needed to get rid of the if (closing == DialogResult.Yes). I'm such a newbie. Thank you!
kmllev 30-Aug-14 10:36am    
Oh wait wait, I ran it several times and just noticed that the application doesn't exit when you click on Yes? It closes yes, but it doesn't exit.
kbrandwijk 30-Aug-14 10:51am    
I have updated my solution to check the CloseReason to prevent a double confirmation. Application.Exit fires Closing Event again.

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