How can I stop the message box from showing up twice when I press the X on the form
? FYI the butoon click works fine it’s the X that prompts me twice.
private void xGameForm_FormClosing(object sender, FormClosingEventArgs e)
{
//Yes or no message box to exit the application
DialogResult Response;
Response = MessageBox.Show("Are you sure you want to Exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (Response == DialogResult.Yes)
Application.Exit();
}
public void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
At the point you make the
Application.Exitcall the form is still open (the closing event hasn’t even finished processing). The Exit call causes the forms to be closed. Since the form is not yet closed it once again goes through theClosingpath and hits your event handler.One way to work around this is to remember the decision in an instance variable