A situation happened

this.FormClosing += btn_close_Click; void btn_close_Click(object sender, EventArgs e) { if (MessageBox.Show(this, "realy exit?", "closing program", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { this.Close(); } } 

After the modal window, the program still turns off.

Tell me how to fix it.

  • 2
    Take a look here - WebMorda
  • add else and e.Cancel = true; Yes, and in the event handler you should have not just EventArgs e , but what should be. - Bulson
  • Thank you now my game is beautiful - Mikola Mirchuk
  • one
    Indeed? Although, yes, if you can get out of the game, then it is really beautiful. - Bulson
  • It is necessary to do so that the exit from the game was a gift in case of victory - Mikola Mirchuk

1 answer 1

It should be something like this:

 void btn_close_Click(object sender, FormClosingEventArgs e) { if (MessageBox.Show(this, "realy exit?", "closing program", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { this.Close(); } else { e.Cancel = true; } } 

CancelEventArgs.Cancel property - returns or sets a value indicating whether the event should be canceled.

Useful links:

PS: if a solution is found and it can help other members of the community in the future, it’s accepted to give a decision and mark it as correct. Especially if the question is quite relevant and did not pick up the minuses.

  • one
    this.Close is optional, since we are this.FormClosing += ... closing ( this.FormClosing += ... ). Here a single-line solution is e.Cancel = MessageBox.Show(...) != DialogResult.Yes; : e.Cancel = MessageBox.Show(...) != DialogResult.Yes; . - Uranus