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 us
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);
}
CloseReason is an enum with the following members:
None
WindowsShutDown
MdiFormClosing
UserClosing
TaskManagerClosing
FormOwnerClosing
ApplicationExitCall
Which leads me to believe that it is dependent on conditions related to the behavior of forms in general (ex. closing a parent form closes it's childs). So in order for the arg to have say, the CloseReason.ApplicationExitCall, the event must be triggered from a Application.Exit call.
Same with say a CloseReason.FormOwnerClosing, assuming your child forms are called using form.Show(Parent)
If your goal is to simply have extra information of why the form is closing, you can simply store it inside the form object as a public attribute or property to access it later, assuming said form wasn't disposed.
Providing what was your motivation in changing the CloseReason would probably help, as well.
CloseReason msdn page for reference http://msdn.microsoft.com/en-us/library/system.windows.forms.closereason.aspx
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.
The way I have started doing this is to set the DialogResult property of the form to different things based on what the user has clicked on the form.
In your button click method:
private void FillOrder_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
// this.Close() is called automatically when you set DialogResult
// so the above line will close the form as well.
}
This way you can do the following in the FormClosing methods:
private void Form_FormClosing(object sender, FormClosingEventArgs e)
{
switch (e.CloseReason)
{
case CloseReason.UserClosing:
switch (this.DialogResult)
{
case DialogResult.OK:
// User has clicked button.
break;
case DialogResult.Cancel:
// User has clicked X on form, show your yes/no/cancel box here.
// Set cancel here to prevent the closing.
//e.Cancel = true;
break;
}
break;
}
}
As far as the CloseReason always being set to UserClosing, it is set to this value by any action that a user can initiate, can't remember exactly what but I'm pretty sure even a task manager force kill is user closing. However I can confirm the other enum values are set in various cases such as a shutdown/reboot while the app is still running. You can even stop windows shutting down by catching ALL close reasons in the switch and cancelling the close.