Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting programmatically closereason

Tags:

c#

winforms

I want to set the CloseReason of a form after I call This.Close() inside the form.

Usually, this forms is closed by itself calling This.Close(), but I want to ask the user if they REALLY want to close the form, and send a mbox with some info. But I have this:

protected override void OnFormClosing(FormClosingEventArgs e)
    {
        if (e.CloseReason == CloseReason.UserClosing)
        {
            if (MessageBox.Show("¿Desea Salir realmente?\nLa factura aun no ha sido pagada por lo que volverá a la pantalla anterior y podrá seguir agregando productos") == DialogResult.No)
            {
                e.Cancel = true;
            }
        }
        base.OnFormClosing(e);
    }

But every time I call This.Close(); the CloseReason is always UserClosing.

Can I set it after the call or I have to handle the OnFormClosing different?

like image 907
josecortesp Avatar asked Dec 28 '09 17:12

josecortesp


2 Answers

Rather than creating the extra variable:

appClosing = true; 
this.Close();

You can call:

Application.Exit();

And then e.CloseReason will equal

CloseReason.ApplicationExitCall

Which might be what you're after.

like image 101
Leon Bambrick Avatar answered Oct 20 '22 09:10

Leon Bambrick


I don't think you can do that, what i always do is to use a flag

appClosing = true;
this.Close();

And then check for that:

protected override void OnFormClosing(FormClosingEventArgs e)
    {
        if (e.CloseReason == CloseReason.UserClosing && !appClosing)
        {
            if (MessageBox.Show("¿Desea Salir realmente?\nLa factura aun no ha sido pagada por lo que volverá a la pantalla anterior y podrá seguir agregando productos") == DialogResult.No)
            {
                e.Cancel = true;
            }
        }
        base.OnFormClosing(e);
    }
like image 25
albertein Avatar answered Oct 20 '22 08:10

albertein