The fact that your ParentForm is inside the panel is definitely your problem.
If I understand your case, you have the following controls / forms:
MainForm is the main form of your application.Panel1 - a panel that is a control inside MainFormTheParentForm is a form inside Panel1 (with TopLevel as false)UserControl1 is a usercontrol inside TheParentFormReportViewer is Microsoft user control inside UserControl1
When you close MainForm , TheParentForm does not close because it is not a top-level form. What you can do is add the following code to MainForm n in the FormClosing event FormClosing :
private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { foreach (Control ctrl in this.Panel1.Controls) { Form ctrlAsForm = ctrl as Form; if (ctrlAsForm != null) { ctrlAsForm.Close(); } } }
This will go through all the controls inside Panel1 and try to call the Close() method explicitly if the control is a form. Thus, closing the main form will also close the forms without the top level, which are inside Panel1 .
Now, as you wrote in UserControl1 :
this.ParentForm.FormClosing += delegate { reportViewer.LocalReport.ReleaseSandboxAppDomain(); };
everything should be OK; when TheParentForm is closed, you can clean up and avoid your exception.
ken2k source share