Re: showdialog and 'Cannot access a disposed object'

Greg Robinson <[email protected]> Fri, 4 Jan 2008 10:48:24 -0500
Newsgroups gmane.comp.windows.devel.dotnet.winforms
Message-ID <[email protected]>
"rpt" has been disposed so it's no longer available.


Greg Robinson
Custom Data Systems, Inc.
www.cds-am.net

-----Original Message-----
From: Discussion forum for developers using Windows Forms to build apps
and controls [mailto:[email protected]]On Behalf Of
chris Burgess
Sent: Friday, January 04, 2008 10:28 AM
To: [email protected]
Subject: [DOTNET-WINFORMS] showdialog and 'Cannot access a disposed
object'


I have the following code to launch forms based upon a selection from
a list box:

 Private Sub LoadReport_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles LoadReport.Click
        Dim rpt As New BaseReport
        rpt = DirectCast(Me.Reports.SelectedValue, BaseReport)
        rpt.ShowDialog()
        rpt.Dispose()
    End Sub

'Reports' is a list box that contains 'Report' objects. The Report
object has a report name property (the DisplayMember) and a report
form property, each of which is derived from BaseForm (the
.valuemember).

If I launch a report form, close it, then try to launch it again, I
get an error:

System.ObjectDisposedException was unhandled
  Message="Cannot access a disposed object.
Object name: 'Report_OrdersByDate'."
  ObjectName="Report_OrdersByDate"
  Source="System.Windows.Forms"
  StackTrace:
       at System.Windows.Forms.Control.CreateHandle()
       at System.Windows.Forms.Form.CreateHandle()
       at System.Windows.Forms.Control.get_Handle()
       at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
       at System.Windows.Forms.Form.ShowDialog()
       at Converse.MainMenu.LoadReport_Click(Object sender, EventArgs
e) in C:\Work\StoreReporter\StoreReporter\MainMenu.vb:line 48
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m,
MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&
m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&
msg)
       at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.Unsaf
eNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
       at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
       at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun(
)
       at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoAppl
icationModel()
       at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(St
ring[]
commandLine)
       at Converse.My.MyApplication.Main(String[] Args) in
17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()