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

Posted on 2004-11-05
Last Modified: 2008-01-09

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.
Question by:DotNetLover_Baan
    LVL 37

    Expert Comment

    why not just move the "global" object out of the form or make it a static member so its actually global ?
    LVL 37

    Expert Comment

    Also if you have a reference to the form you can check if it is disposed, if it is create a new one.
    LVL 44

    Accepted Solution

    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

    LVL 44

    Expert Comment

    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.

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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

    LVL 2

    Expert Comment

    >> 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.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now