Opening and Closing a form error : Cannot access a disposed object named "frmImage".

D

D Witherspoon

I use the following code to open up a form.

-------------------------------------------------------

If fImage Is Nothing Then
fImage = New frmImage
End If

fImage.Show()

-------------------------------------------------------

It works the first time, but when the form is closed and this piece of code runs again I want to check to see if it is nothing and then if it is nothing then create a new instance of the form and show it. Otherwise just show the form. Well when the form is closed by the user the form becmome "disposed" instead of nothing. Is this the result of ..NET Garbage Collection, and that fImage would eventually become nothing?

On top of this, how can I check to see if the object is disposed?



This is the error when closing the form and running that code above again:

Run-time exception thrown : System.ObjectDisposedException - Cannot access a disposed object named "frmImage".
 
M

Morten Wennevik

Hi D Witherspoon,

Yes, when an object like windowsform is closed it will be marked disposed
until the garbage collector has released it completely, at which time it
will be null.

You can check if Windows Form is disposed by checking its IsDisposed
property

if(fImage == null || !fImage.IsDisposed)
fImage = new frmImage();
 
H

Herfried K. Wagner [MVP]

fImage = New frmImage
End If

fImage.Show()

-------------------------------------------------------

It works the first time, but when the form is closed and this piece of code
runs again I want to check to see if it is nothing and then if it is nothing
then create a new instance of the form and show it. Otherwise just show the
form. Well when the form is closed by the user the form becmome "disposed"
instead of nothing. Is this the result of .NET Garbage Collection, and that
fImage would eventually become nothing?

On top of this, how can I check to see if the object is disposed?



This is the error when closing the form and running that code above again:

Run-time exception thrown : System.ObjectDisposedException - Cannot access a
disposed object named "frmImage".<<<

See:

<URL:http://groups.google.de/[email protected]>
 

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