Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5743
  • Last Modified:

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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.

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now