This problem occurs on Windows 2000 but NOT on Windows XP:
If you programmatically remove a control from the Controls collection of a
panel on another form, then that form's default WndProc will return FALSE to
the WM_QUERYENDSESSION message!
Is there anything that can be done to fix this other than overriding the
WndProc (which I don't want to do for performance reasons)?
Please help!
To reproduce: (on Win 2000 obviously)
VS.Net 2003, Framework 1.1 w/ service packs:
In VS.Net, create a C# win forms application with two Forms (e.g. Form1 and
Form2):
On Form1, drop two buttons.
On Form2, drop a panel, drop a button inside the panel. In Form2 code, make
panel1 be public.
In Form1, add a Form2 private variable (e.g. call it "form2"), then write
the first button's click handler:
if (this.form2 == null) this.form2 = new Form2();
this.form2.Show();
The second button's click handler:
this.form2.panel1.Controls.RemoveAt(0);
Then run it... click the first button, click the second button. You should
see the button disappear from the panel of form2. Then try to exit Windows
(or, a bit nicer, write something to FindWindow of the Form2 window and send
it WM_QUERYENDSESSION).
Thanks!,
Greg Patrick
Stoitcho Goutsev \(100\) [C# MVP] - 20 Apr 2005 23:56 GMT
Hi Greg,
I'm surprised that it doesn't happen in XP.
I believe this is one well know bug in windows forms. The bug shows up when
you remove a focused control. The framework doesn't clean up correctly and
tries to validate the control upon closing. It fails because the control is
not there. Because of this closing gets cancel.
To workaround this you have options:
1. To swhitch the focus on another control before taking the control off - I
personally dislike this.
2. To call Form.OnControlRemoved (and pass a reference to the control) prior
or after removing the control. If you call control.Dispose you have to call
OnControlRemove before disposing the control.
HTH

Signature
Stoitcho Goutsev (100) [C# MVP]
> This problem occurs on Windows 2000 but NOT on Windows XP:
> If you programmatically remove a control from the Controls collection of a
[quoted text clipped - 35 lines]
>
> Greg Patrick
Dmytro Lapshyn [MVP] - 21 Apr 2005 07:20 GMT
Hi all,
#2, as far as I remember, is a kind of 'official' workaround (I even believe
it's documented somewhere in the KB).
Well, at least it works great for me :-)

Signature
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
> Hi Greg,
>
[quoted text clipped - 55 lines]
>>
>> Greg Patrick
Greg Patrick - 21 Apr 2005 17:37 GMT
Thanks very much, I'll try out your suggestions.
Greg
> Hi Greg,
>
[quoted text clipped - 55 lines]
>>
>> Greg Patrick
Greg Patrick - 21 Apr 2005 18:12 GMT
One more note... it appears the reason it wasn't happening in XP is
basically the same reason it *was* happening in 2000... the 2000 machine I
was using didn't have the framework 1.1 service pack, while the XP machine
did.
It is not a problem on either platform with the service pack...
Still, I might want to support pre-service pack users, so thanks again...
> Thanks very much, I'll try out your suggestions.
>
[quoted text clipped - 59 lines]
>>>
>>> Greg Patrick