473,378 Members | 1,479 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

cannot access a disposed object

I'm launching one window from another. I have a close button in the first
window and when I click it, it executes me.close. However, the form also
has a treeview control. If I launch the second window on a treeview node
selection, the second window launches just fine; however, if I have me.close
in the code right after it, I get the error 'cannot access a disposed object
called 'treeview'. This even happens if I simply call closeit.performclick.

If I do nothing, I can click the close button and I don't get an error.
What's going on here?

Tx for any help.

Bernie Yaeger
Nov 20 '05 #1
2 1952
Hi Bernie,

This is one that crops up a lot. When you are in the event handler for a
control such as a TreeView (last time it was a DataGrid), you are deep in the
call stack (have a look at the stack trace).

When your handler exits, control goes back up this stack of calls as they
in their turn finish off what they were doing and exit. The effect of Me.Close
is to Dispose of the Form, including all of the Controls that it contains.
Bye-bye Form, bye-bye TreeView.

The trouble is, the TreeView event-handling code is somewhere in that
stack waiting to do its finishing off - and there's no TreeView anymore. Bang!

One solution is to not close the Form in the event handler. Not what you
want, I imagine. Another is to hide it and close it by some other means. One
of these is to have a Timer (use System.Timers) set to go off in a few
millisecs. The Tick event handler would stop the Timer and then close the
Form.

Regards,
Fergus
Nov 20 '05 #2
Hi Fergus,

Tx - you explained it very well. I'll try the timer approach.

Bernie
"Fergus Cooney" <fi*****@post.com> wrote in message
news:On**************@tk2msftngp13.phx.gbl...
Hi Bernie,

This is one that crops up a lot. When you are in the event handler for a control such as a TreeView (last time it was a DataGrid), you are deep in the call stack (have a look at the stack trace).

When your handler exits, control goes back up this stack of calls as they in their turn finish off what they were doing and exit. The effect of Me.Close is to Dispose of the Form, including all of the Controls that it contains.
Bye-bye Form, bye-bye TreeView.

The trouble is, the TreeView event-handling code is somewhere in that
stack waiting to do its finishing off - and there's no TreeView anymore. Bang!
One solution is to not close the Form in the event handler. Not what you want, I imagine. Another is to hide it and close it by some other means. One of these is to have a Timer (use System.Timers) set to go off in a few
millisecs. The Tick event handler would stop the Timer and then close the
Form.

Regards,
Fergus

Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: salih ataoz | last post by:
i close a form and i want to open it again it show me Cannot access a disposed object at vb6 this is ok at comman1_click form1.show
7
by: Ryan Park | last post by:
Hi, //SITUATION I got a panel control that hold a certain position on a form. Every controls or UIs are on this panel. At certain situation, I called dispose() method of this panel control...
5
by: D Witherspoon | last post by:
I use the following code to open up a form. ------------------------------------------------------- If fImage Is Nothing Then fImage = New frmImage End If fImage.Show()
5
by: theinvisibleGhost | last post by:
I'm having a problem that occurs at random in my app. I get an exception "Cannot Access a disposed object" In MSCorLib when calling boolean Change (int32, int32) Stack trace reveals...
5
by: mthgk | last post by:
I have a C# MDI app. The child forms do alot of work, so this work is perfomed on a different thread created using ThreadPool.QueueUserWorkItem(). Because the status of the work is important to...
3
by: Tracey | last post by:
sorry I post this problem again. I have to stop my work to fix the problem. I'm doing a multi form application(Not a MDI one). My startup form say Form1 has a datagrid say datagrid1, when I...
2
by: Rajat Tandon | last post by:
Hi, I have a grid which is continuously updating by the data from a external event. When I close the form on which the grid is placed, then it gives the error message ... "Can not access a...
1
by: Amit Dedhia | last post by:
Hi I am having problem working with Timers in C++/CLI (the .NET version of C++) I have an application which has several forms with pictureBox controls on it. There is a background timer...
0
by: sdanda | last post by:
Hai i am working on vb.net. In my application I created four forms.Those are first.vb,f1.vb,f2.vb and f3.vb In firstvb I added 3 checkboxes and a "display" button.The 3 checkboxes are used to...
4
JustRun
by: JustRun | last post by:
Hi All, I'm developing a windows desktop solution using VC# , I deal with my database using Dataset. My Problem that i'm trying to call a Form to display a confirmation message after every...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.