What can cause a form not to close itself?
I wonder just what can cause a form not to close.
I have a big form, with several TabControls, DataGridViews and many DataBound-Controls, at least 10 BindingSources are involved. (Thats the point why I can't post my code here, sorry).
Now, I have the problem, that somewhere in development (just refaktoring) the form stopped closing correctly. When I click on the Close-Button (Red-Cross-Box) I goes through FormClosing and FormClosed but then nothing more happens. VisualStudio2008 with debugging and halt on all Errors when thrown doesn't event mention something went wrong, the form just stays where it is.
What could cause such a behavior? It is NOT that e.Cancel in the FormClosing-Event is set!
After step by step merging my changes to another clean working copy, the form closes correctly, but an exception is thrown:
ArgumentNullException with Message: "The value cannot be null. Parametername: component". It is thrown in Form.Designer.Dispose upon calling the base.Dispose(disposing) line.
Seems to be something with the DataBinging, any hints welcome.
I'll put in the StackTrace, it is really any of the DataBound Controls, from what I understand out of the StackTrace, it is a TextBox - I do not get Framework Source Stepping enabled, so I can not figure out what TextBox breaks here.
bei System.ComponentModel.ReflectPropertyDescriptor.RemoveValueChanged(Object component, EventHandler handler)
bei System.Windows.Forms.BindToObject.CheckBinding()
bei System.Windows.Forms.Binding.CheckBinding()
bei System.Windows.Forms.Binding.SetBindableComponent(IBindableComponent value)
bei System.Windows.Forms.ControlBindingsCollection.ClearCore()
bei System.Windows.Forms.BindingsCollection.Clear()
bei System.Windows.Forms.ControlBindingsCollection.Clear()
bei System.Windows.Forms.Control.ResetBindings()
bei System.Windows.Forms.Control.Dispose(Boolean disposing)
bei System.Windows.Forms.TextBox.Dispose(Boolean disposing)
bei System.ComponentModel.Component.Dispose()
bei System.Windows.Forms.Control.Dispose(Boolean disposing)
bei System.ComponentModel.Component.Dispose()
bei System.Windows.Forms.Control.Dispose(Boolean disposing)
bei System.ComponentModel.Component.Dispose()
bei System.Windows.Forms.Control.Dispose(Boolean disposing)
bei System.ComponentModel.Component.Dispose()
bei System.Windows.Forms.Contro开发者_如何学运维l.Dispose(Boolean disposing)
bei System.Windows.Forms.ContainerControl.Dispose(Boolean disposing)
bei System.ComponentModel.Component.Dispose()
bei System.Windows.Forms.Control.Dispose(Boolean disposing)
bei System.ComponentModel.Component.Dispose()
bei System.Windows.Forms.Control.Dispose(Boolean disposing)
bei System.Windows.Forms.TabControl.Dispose(Boolean disposing)
bei System.ComponentModel.Component.Dispose()
bei System.Windows.Forms.Control.Dispose(Boolean disposing)
bei System.Windows.Forms.ContainerControl.Dispose(Boolean disposing)
bei System.Windows.Forms.Form.Dispose(Boolean disposing)
bei My.BaseForm.Dispose(Boolean disposing) in BaseForm.Designer.cs:Zeile 30.
bei My.InheritedForm.Dispose(Boolean disposing) in InheritedForm.Designer.cs:Zeile 25.
bei System.ComponentModel.Component.Dispose()
bei System.Windows.Forms.Form.WmClose(Message& m)
bei System.Windows.Forms.Form.WndProc(Message& m)
bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
bei System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Alright, got it, with Framework source stepping working (why ever it is so complicated) I could figure out what databinding failed. It was a databinding to a property of an object that was null at runtime. Thanks for the help.
PS: I'm sorry for those getting angry about me, but i stated this question explicitly for not getting those all-day answers. It is nice to remind me of these possible failures, but if I do state that it is not with this, don't be childisch to pretend on this.
I think one of your 10 binding sources has an error (in one of your many databound controls) and isn't releasing you from a field somewhere. VS isn't very transparent if there are errors in databinding and problems do get silently swallowed.
Although, if this is the case:
When I click on the Close-Button (Red-Cross-Box) I goes through FormClosing and FormClosed
then that somewhat contradicts my theory.
Why not try decoupling the databinding piece by piece and seeing if you can then close the form?
an example of how to prevent a form to be closed:
protected override void OnFormClosing(FormClosingEventArgs e)
{
e.Cancel = true;
base.OnFormClosing(e);
}
maybe this form is inherited and a base class prevent the form closing when some conditions are not satisfied.
Finally, if you don't achieve any solution, you can debug through the .NET Framework code and see what happens in the your form's OnClosing method.
Form can be prevented from closing even from your Form_Closing event handler.
private void Form1_Closing(Object sender, CancelEventArgs e)
{
e.Cancel = true;
}
Check whether you're setting e.Cancel to true .. anywhere in the code which is preventing it from closing.
精彩评论