Cannot access disposed object

T

Tracey

Hi, there
I met a very wierd problem. My startup form say Form1 has
a datagrid say datagrid1, when I click an item in
datagrid1, I wanna show form2. In my code, I achieved
that with:
private sub Datagrid1_currentCellChanged(ByVal sender As
Object, ByVal e As EventArgs)
....
Me.close()
dim frm2 as new form2()
frm2.show()
end sub

In form2 there is also a datagrid called datagrid2.

My problem is when I click the item in form1,form2 showed
up but with an error massage window says:
Cannot access disposed object named "DataGridTextBox"
Object name: "DataGridTextBox"
The problem is I didn't have any datagrid named with
datagridTextBox. Can someone provide some advice ?


Thanks a lot!
 
T

Tom Spink

Hi, calling Me.Close will dispose the form, and put it out for garbage
collection. Use Me.Hide instead.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
F

Fergus Cooney

Hi Tracey,

I think it's likely that when you call Me.Close, it disposes of Me along
with all the controls on it, including Datagrid1. DataGridTextbox is probably
a control associated with DataGrid1 and it will be disposed of as well.

The problem is, this is happening inside an eventhandler for the DataGrid
and it expects, when it returns, to find itself with a live and valid
DataGrid. It doesn't, so Bang!

See if taking out the Me.Close makes the problem go away. If it does,
you'll hav to arrange a different way to close the form. One idea is to start
a timer with a very short delay and close the form when it goes off.

Another. It might be possible to pass Me to frm2 in the form2 constructor
and have <it> close form1 but it depends on whether closing another form is
done immediately or via its message queue, and I'm not certain.

Regards,
Fergus
 

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