Cannot access to disposed oject.

T

Tracey

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 click a grid item in datagrid1, I wanna show
form2. In my code, I achieved that with:

Public class Form1
Inherits System.Windows.Forms.Form
DIm frm2 as Form2
...
Private sub Datagrid1_currentCellChanged(ByVal sender
As Object, ByVal e As EventArgs)
Dim bm As BindingManagerBase =
DataGridBTerm.BindingContext(DataGridBTerm.DataSource,
DataGridBTerm.DataMember)
Dim datarw As DataRow = CType(bm.Current,
DataRowView).Row
//click an item
If sender.currentcell.columnnumber = 0 Then
Me.close()
frm2 = new form2
End sub
.....
End Class

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"
Here is the debug information:
************** Exception Text **************
System.ObjectDisposedException: Cannot access a disposed
object named "DataGridTextBox".
Object name: "DataGridTextBox".
at System.Windows.Forms.Control.CreateHandle()
at System.Windows.Forms.TextBoxBase.CreateHandle()
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.TextBox.GetLength()
at System.Windows.Forms.TextBoxBase.set_SelectionStart
(Int32 value)
at System.Windows.Forms.TextBox.SelectInternal(Int32
start, Int32 length)
at System.Windows.Forms.TextBoxBase.SelectAll()
at System.Windows.Forms.DataGridTextBoxColumn.Edit
(CurrencyManager source, Int32 rowNum, Rectangle bounds,
Boolean readOnly, String instantText, Boolean
cellIsVisible)
at System.Windows.Forms.DataGrid.Edit(String
instantText)
at System.Windows.Forms.DataGrid.Edit()
at System.Windows.Forms.DataGrid.set_CurrentCell
(DataGridCell value)
at System.Windows.Forms.DataGrid.OnMouseDown
(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseDown(Message&
m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage
(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc
(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


Any help on that will be appreciated.
Thanks!
 
C

Cor

Hi Tracey,

Can this be the error, just a guess
Me.close()
frm2 = new form2

For me it looks strange

Close the class and when it is closed call with that closed class another
class?

Maybe I am wrong, but I find it looking strange?

Cor
 
A

Armin Zingler

Tracey said:
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 click a grid item in datagrid1, I wanna show
form2. In my code, I achieved that with:

Public class Form1
Inherits System.Windows.Forms.Form
DIm frm2 as Form2
...
Private sub Datagrid1_currentCellChanged(ByVal sender
As Object, ByVal e As EventArgs)
Dim bm As BindingManagerBase =
DataGridBTerm.BindingContext(DataGridBTerm.DataSource,
DataGridBTerm.DataMember)
Dim datarw As DataRow = CType(bm.Current,
DataRowView).Row
//click an item
If sender.currentcell.columnnumber = 0 Then
Me.close()
frm2 = new form2
End sub
....
End Class

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"
[...]

Just a thought and an attempt: add a Timer (enabled=false) to your Form.
Instead of closing the Form in the currentCellChanged event handler, _only_
enable the timer. In the timer's tick event, close the Form. Maybe the code
raising the event is trying to access the Form after you've closed it. The
Timer is a way to find this out. AFAIR there was a similar problem in the
past.
 
T

Tom Spink

Hi Tracey,
Me.close()

This is the beast. You need to change it to 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
 

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