Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form closed but visible

Why a form is still visible after a FormClosed event is raised? How to detect when a form is actually closed?

enter image description here

Interesting part is that

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

leads to the same result.

like image 421
aush Avatar asked Dec 17 '13 10:12

aush


2 Answers

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.

like image 136
Hans Passant Avatar answered Nov 18 '22 12:11

Hans Passant


The thing is that you're showing a modal dialog - which prevents the UI thread from actually removing the form from the screen.

like image 2
Thorsten Dittmar Avatar answered Nov 18 '22 13:11

Thorsten Dittmar