Dont want Form object to dispose on Form.Close()


Is there anyway to ensure that a form object is not disposed when the form is closed? the problem is that i have a global form object and it is opened from other forms. If the open form is closed in any of these forms, the global oject is disposed and can't be used any more without the whole application being re-executed.
LVL 18
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

why not just move the "global" object out of the form or make it a static member so its actually global ?
Also if you have a reference to the form you can check if it is disposed, if it is create a new one.
rather than closing the form, simply HIDE it (Form.Hide).  That way it is still loaded in memory, and is still 'active'. to the GC will not dispose of it.

Also, you can test to see if the form object points to anything, and if it doesnt, reload it:

If frmMyGlobalForm is Nothing then
   frmMyGlobalForm = new frmGlobal
end if


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

when you close a form, you are telling the GC that you no longer need that form, and that when it is necessary, it is ok to Garbage Collect it.  But what you are asking says the even when you close the form, you may want to get it back again, so you really don't want to close it.  Either you are through with the form (then close it), or you aren't (then don't close it, HIDE it) - you can't have it both ways.

Mike TomlinsonMiddle School Assistant TeacherCommented:
If you want the form to hidden instead of closed when the X is clicked in the top right, then do something like this:

Public Class Form2
    Inherits System.Windows.Forms.Form

    Private Sub Form2_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        e.Cancel = True
    End Sub

End Class

>> Is there anyway to ensure that a form object is not disposed when the form is closed?

For forms opened with ShowDialog, the answer is No since Close will call Dispose automatically.

If the form is not opened modally, then Dispose will not be closed automatically.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.