private void Form1_FormClosing(object sender, FormClosingEventArgs e){ if (Flag == true) { var formClose = MessageBox.Show("Вы точно хотите завершить работу ?", "FormClose", MessageBoxButtons.YesNo); if (formClose == DialogResult.Yes) { Exit(); KILL(); } if(formClose == DialogResult.No) { } } } 

No matter what you click, the program will still close. Why is that?

  • Verification needs to be done here. This event is called when the form is closed. That is, the closing has already begun, and this is what you want to do with it. The dialogue must be shown before this event. - Vladimir Paliukhovich
  • Can you tell me how to show the dialogue before the event? - user209821
  • try in the last if to make a return . - MaximK
  • 2
    You were given the answer in another question of yours: set the e.Cancel property. - Alexander Petrov

1 answer 1

You must set the e.Cancel property to true so that the form does not close:

 if(formClose == DialogResult.No) { e.Cancel = true; }