TreeView and ObjectDisposedException

V

Vitaly Sedov

Hi All!

I have an application that opens form with TreeView.
I want to close form by AfterSelect Event:

private void MyTreeView_AfterSelect(object sender,
System.Windows.Forms.TreeViewEventArgs e)
{
this.Close();
}

But everytime when I clicking on TreeView Item,
I get following error:

An unhandled exception of type 'System.ObjectDisposedException' occurred in
system.windows.forms.dll
Additional information: Cannot access a disposed object named "TreeView".

Any ideas?

Thank you,
Vitaly Sedov
 
G

Guest

Hi Vitaly

It looks like the AfterSelect event handler is trying to call CreateHandle() on the TreeView object after it has been disposed by this.Close()

You should inherit your own tree from TreeView Class, and override CreateHandle() method of that class to not call the base method if the object has already been disposed

public class myTree : System.Windows.Forms.TreeVie

protected override void CreateHandle(

if(!this.IsDisposed
base.CreateHandle ();



Hope this helps
Igor Zinkovsk
Visual Studio Build/Release Tea

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.ht

----- Vitaly Sedov wrote: ----

Hi All

I have an application that opens form with TreeView
I want to close form by AfterSelect Event

private void MyTreeView_AfterSelect(object sender
System.Windows.Forms.TreeViewEventArgs e

this.Close()


But everytime when I clicking on TreeView Item
I get following error

An unhandled exception of type 'System.ObjectDisposedException' occurred i
system.windows.forms.dl
Additional information: Cannot access a disposed object named "TreeView"

Any ideas

Thank you
Vitaly Sedo
 
V

Vitaly Sedov

It's really helps!
Thank you!

Vitaly Sedov

Igor Zinkovsky said:
Hi Vitaly.

It looks like the AfterSelect event handler is trying to call
CreateHandle() on the TreeView object after it has been disposed by
this.Close().
You should inherit your own tree from TreeView Class, and override
CreateHandle() method of that class to not call the base method if the
object has already been disposed:
public class myTree : System.Windows.Forms.TreeView
{
protected override void CreateHandle()
{
if(!this.IsDisposed)
base.CreateHandle ();
}
}

Hope this helps,
Igor Zinkovsky
Visual Studio Build/Release Team

This posting is provided "AS IS" with no warranties, and confers no
rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top