Why a form is still visible after a FormClosed event is raised? How to detect when a form is actually closed?
Interesting part is that
_form2.VisibleChanged += (s, a) =>
{
if (_form2.Visible == false)
MessageBox.Show("TEXT");
};
leads to the same result.
You are closing the dialog in an unusual way, the normal way it is done is by setting the form's DialogResult property. Winforms does still synthesize the FormClosed event in this case but does it at the "wrong" time, the window is still visible. It will go invisible immediately afterwards.
If you need a workaround for this then that's possible, the trick is to delay whatever you want to do in your FormClosed event handler. That's elegantly done by using the Control.BeginInvoke() method, like this:
_form2.FormClosed += (s, a) => {
this.BeginInvoke(new Action(() => MessageBox.Show("TEXT")));
};
And you'll now see the MessageBox after the window disappeared.
Beware of the bug in your code, you subscribe the FormClosed event more than once.
The thing is that you're showing a modal dialog - which prevents the UI thread from actually removing the form from the screen.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With