Re: showdialog and 'Cannot access a disposed object'
Mike Andrews <[email protected]> Fri, 4 Jan 2008 09:56:41 -0600
| Newsgroups | gmane.comp.windows.devel.dotnet.winforms |
|---|---|
| Message-ID | <[email protected]> |
The issue is that you're creating a new Report via this line: Dim rpt As New BaseReport and then destroying it and in place of it setting an existing one: rpt = DirectCast(Me.Reports.SelectedValue, BaseReport) since the list box contains the references to the report object, and you've previously disposed it it will fail to run. You need to refrain from calling the dispose() method until you're done with the form in which the listbox resides. You can then dispose all of the reports. Also, change the first two lines of code to this: Dim rpt As BaseReport = DirectCast(Me.Reports.SelectedValue, BaseReport) or better still... DirectCast(Me.Reports.SelectedValue, BaseReport).ShowDialog() and leave off the dispose until it should be disposed. On Jan 4, 2008 9:28 AM, chris Burgess <[email protected]> wrote: > 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.UnsafeNativeMethods.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.DoApplicationModel > () > at > Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run > (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() >