The form is closed but visible

Why is the form still visible after the FormClosed event occurs? How to determine when the form is actually closed?

enter image description here

The interesting part is that

_form2.VisibleChanged += (s, a) => { if (_form2.Visible == false) MessageBox.Show("TEXT"); }; 

leads to the same result.

+6
source share
2 answers

You close the dialog in an unusual way, in the usual way this is done by setting the DialogResult form property. Winforms still synthesizes the FormClosed event in this case, but does it at the β€œwrong” time, the window is still visible. He will immediately become invisible.

If you need a workaround for this, then this is possible, the trick is to defer everything you want to do in the FormClosed event handler. This is elegantly done using the Control.BeginInvoke () method, for example:

  _form2.FormClosed += (s, a) => { this.BeginInvoke(new Action(() => MessageBox.Show("TEXT"))); }; 

And now you will see MessageBox after the window disappears.

Beware of errors in the code, you sign the FormClosed event more than once.

+3
source

The fact is that you are showing a modal dialog that prevents the actual removal of the user interface stream from the screen.

+2
source

All Articles