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

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

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

Hi..

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.
0
DotNetLover_Baan
Asked:
DotNetLover_Baan
1 Solution
 
gregoryyoungCommented:
why not just move the "global" object out of the form or make it a static member so its actually global ?
0
 
gregoryyoungCommented:
Also if you have a reference to the form you can check if it is disposed, if it is create a new one.
0
 
Arthur_WoodCommented:
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

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

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

AW
0
 
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
        Me.Hide()
    End Sub

End Class

~IM
0
 
mjwillsCommented:
>> 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.
0

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