Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / April 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
D Witherspoon - 08 Apr 2005 15:26 GMT
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".
Cor Ligthert - 08 Apr 2005 16:05 GMT
D. Witherspoon

Setting something to nothing does nothing to delete it.
It only deletes the reference it has to its object

When you want to know if you still can use a form than you can use the

Isdisposed.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlr
fsystemwindowsformscontrolclassisdisposedtopic.asp


I hope this helps,

Cor
Morten Wennevik - 08 Apr 2005 16:11 GMT
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();

Signature

Happy Coding!
Morten Wennevik [C# MVP]

Herfried K. Wagner [MVP] - 08 Apr 2005 18:37 GMT
"D Witherspoon" <dwitherspoon@noway.org> schrieb:
>>>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".<<<

See:

<URL:http://groups.google.de/groups?selm=eILSwL4CFHA.2072%40TK2MSFTNGP10.phx.gbl>

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.