views:

202

answers:

3

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.Control.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.

+1  A: 

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.

serhio
Thank you, but that was not what I intended whith my question, I do not need to know how I can prevent this, but how this can be accidently/unwanted prevented.
BeowulfOF
thank you to down-vote my try to help. SEARCH in your code `Cancel = true` by e.g., dude. or inspect your OnClosing events-overrides.
serhio
1. I'm happy about every answer.2. Did you get a message who downvoted you?3. You answer does not answer my question - so a downvote would be quite correct.
BeowulfOF
@Beowulf, so, if a answer is not an answer you should downvote more that 90% of answers in this site.
serhio
Doin anything just because everyone is doing so is allways wrong.
BeowulfOF
+1 for clear example of might have been wrong. @BeowulfOF: Maybe it's time to stop breathing then? Downvoting is for bad or misleading answers, not for suggestions that didn't answer your question. Just don't mark it as accepted and go on with your life :-)
Zano
@Zano: This answer is misleading, as I did not asked for what can I do to prevent a form from closing, but what can prevent a form of doing that.
BeowulfOF
@BeowulfOF: You learn a lot from knowing what causes a condition or behavior, especially when you're trying to find out how to stop it. Ask any mechanic, doctor or programmer.
Zano
A: 

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.

AB Kolan
+1  A: 

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?

hawbsl
Good Idea, I'll try this out. I think it must be something that's got a hold on the form...
BeowulfOF
As you can see with the StackTrace I got after demasking the failure, it is really something with databinding. Thank you for the hint.
BeowulfOF