We created a new form that we're showing via ShowDialog
and added a "Cancel" button to it. Here's how we're opening the form from its parent:
// _modalForm is a class-level instance of our ModalForm class
var result = _modalForm.ShowDialog(this);
MessageBox.Show(result.ToString());
Here's the cancel button's Click
event handler in ModalForm
.
private void btnCancel_Click(object sender, EventArgs e)
{
Close();
}
In our FormClosing
event, we have this code (based on this answer).
private void ModalForm_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
Hide();
_parentForm.RefreshData();
}
Surprisingly, when we click the "Cancel" button (or use the "X" button at the top of the form), the FormClosing
event is raised twice. Both times the CloseReason
is UserClosing
.
I double checked to make sure InitializeComponent
isn't call twice and that we only subscribe to the event once. btnCancel
is not set at the CancelButton
property for the form. It also doesn't have DialogResult
set in the designer. When I check the return value of ShowDialog
though, it is set to DialogResult.Cancel
.
Changing btnCancel_Click
to just be DialogResult = DialogResult.Cancel
instead of Close()
and doing nothing except _parentForm.Refresh()
in the FormClosing
event fixes the issue of the event getting raised twice.
Does anyone know why in this particular scenario the FormClosing
event gets raised twice?