Re: showdialog and 'Cannot access a disposed object'

"Bhatnagar, Amit" <[email protected]> Fri, 4 Jan 2008 08:46:33 -0700
Newsgroups gmane.comp.windows.devel.dotnet.winforms
Message-ID <[email protected]>
you disposed your dialog, how do you expect it to show up again? Create
a new one (via new) and then use it.  

-----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 8: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.U
nsafeNativeMethods.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.On
Run()
       at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Do
ApplicationModel()
       at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Ru
n(String[]
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()

The information contained in this e-mail message is PRIVATE. It may contain confidential information and may be legally privileged. It is intended for the exclusive use of the addressee(s). If you are not the intended recipient, you are hereby notified that any dissemination, distribution or reproduction of this communication is strictly prohibited. If the intended recipient(s) cannot be reached or if a transmission problem has occurred, please notify the sender immediately by return e-mail and destroy all copies of this message. 
Thank you.