I'm having a difficult time trying to track this issue down. Hopefully,
someone might be able to share some insight.
I have a situation where a form is not closed when I explicitly call Close()
on it. As part of trying to debug this problem, I overrode the "OnClosing"
method (and OnFormClosing, too) and noticed that the 'Cancel' member of the
ClosingEventArgs parameter is already set to true upon entering and before I
explicitly forward the call to base.OnClosing(). If I force Cancel to be
false, then the form will close without a problem.
Does anyone have an idea why the framework is setting Cancel to true?
It is difficult to reproduce the problem with a small example. What I can
tell you is that the form in question is a TopLevel (and TopMost) form with
the Owner property set to an MDI child. The TopLevel form has a splitter
control that houses two child forms... however, I remove the splitter control
from the form before I call Close() (the form then contains no child
controls). This is the point where the form fails to close.
Dmytro Lapshyn [MVP] - 05 Apr 2006 07:30 GMT
Hi RitualDave!
If we are lucky, this is a known issue with Windows Forms when a control
that owns the focus is being removed dynamically.
See the link below for suggested resolution:
http://www.pcreview.co.uk/forums/showpost.php?s=63c7c13afc2c8573f183c5b9d19fcce8
&p=3926586&postcount=2
The crucial point is to call OnControlRemove on the parent before removing a
child control.
> I'm having a difficult time trying to track this issue down. Hopefully,
> someone might be able to share some insight.
[quoted text clipped - 20 lines]
> from the form before I call Close() (the form then contains no child
> controls). This is the point where the form fails to close.
RitualDave - 05 Apr 2006 17:55 GMT
Excellent. Thanks, Dmytro!
> Hi RitualDave!
>
[quoted text clipped - 32 lines]
> > from the form before I call Close() (the form then contains no child
> > controls). This is the point where the form fails to close.