Confirmation of closing the form

Asked

Viewed 4,012 times

2

I’m trying to create a confirmation for when the user tries to close a form either with a button, or by the button x, or Alt+F4, or right-click at the top and then click close, etc...

I tried to use the code below but it didn’t work:

private void Form1_FormClosing(object sender, FormClosedEventArgs e)
{
  if (e.CloseReason == CloseReason.UserClosing)
  {
    var result = MessageBox.Show(this, "Você tem certeza que deseja sair?", "Confirmação", MessageBoxButtons.YesNo);

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

3 answers

4


When arriving at the event FormClosing the Form will be closed, except if we set true the property Cancel of the event.

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (e.CloseReason == CloseReason.UserClosing)
        {
            var result = MessageBox.Show(this, "Você tem certeza que deseja sair?", "Confirmação", MessageBoxButtons.YesNo);
            if (result != DialogResult.Yes)
            {
                e.Cancel = true;
            }
        }
    }

Note: If the confirmation message is not displayed, it may be that the event is not triggered because it is not tied to the form. In that case, confirm that the method InitializeComponents() inside the archive Form1.Designer.Cs contains the code snippet:

this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);

  • It didn’t work, it’s like this code doesn’t exist, it just closes.

  • @Patrick should work, unless that form of yours isn’t leading. Is the main ?

  • And is event being triggered? Does the message appear? Also see that the if test is with != and no more ==

  • It is the main form, tested with == and with != , none worked.

  • 1

    So the error message appears or nothing appears? If it doesn’t, the problem is that you simply added the method that should receive the event, but you didn’t attach the event to the form, which should have this code snippet in the Initializecomponents() method inside the Form1.Cs file.: this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);

  • @iuristona Add the content of this last comment to your reply to be complete. ;)

  • @Zuul thought about it, but there were 2 problems and the second I kicked, it does not appear in the question, it is assumed that the event is already tied to the form. Already the code as it was actually asked did not work, it was missing e.Cancel = true

  • @iuristona What I thought was adding you like: "Important Note: If the problem is the fact that the message does not even appear, it is because XXX and you should XXX." So future visitors with the same problem already know that if it appears have to cancel the event, but if the message does not appear it is because you have to attach the event to the form. (But of course, it’s up to you) :)

  • This method is not suitable for closure buttons created by the programmer, right?

  • If you are having problems try to remove this if (e.CloseReason == CloseReason.UserClosing)

Show 5 more comments

0

My code is like this in the main form:

        private void Form1_FormClosing(object sender, FormClosingEventArgs e){
        var result = MessageBox.Show("Deseja realmente sair?", "Rei dos Pisos", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);            
            if (Convert.ToString(result) == "Cancel") { e.Cancel = true; }            
    }

At least it’s working when I click the close button on the form.

-1

     var result = MessageBox.Show("Deseja realmente sair?", "Rei dos Pisos", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);            
            if (Convert.ToString(result) == "Cancel")
 { 
    e.Cancel = true; 
 } else{Aplication.Exit();// faltou só isso

Browser other questions tagged

You are not signed in. Login or sign up in order to post.