How to check a form is still open?

  • Thread starter Thread starter Saul
  • Start date Start date
S

Saul

Hi all,

I have a fairly simple problem. I have a multi form VB app and on startup I create the forms and store them in a public class. A bit of code might help know what I mean...sorry if my naming convention is non-standard!

Public Class myforms
Private Shared gfrmDistribution As frmDistribution

Public Shared Property frmDistribution() As frmDistribution
Get
Return gfrmDistribution
End Get
Set(ByVal Value As frmDistribution)
gfrmDistribution = Value
End Set
End Property
end class
When my app starts up, I use this code to create the form (once), and to display it when needed..

' When app starts up
myforms.frmDistribution = New frmDistribution

' Whenever I need to show the form
myforms.frmDistribution.Show()
Ok, hopefully, so far so good. I know it's a bit convoluted, but stay with me! The problem is that the OS, when it needs memory sometimes closes non-visible windows and my show() fails. So, I want to test to see if the form is still in existence before I try and show it, but I'm not sure how. I tried this code...

if not(myforms.frmDistributon is nothing) then
myforms.frmDistribution.Show() ' Doesn't work...
end if
Is there a way to tell if the form still exists?

Thanks for any help!!

- Saul
 
I think your form variable is getting cleaned up by the garbage collector. I
think you have two options. One, do your form show in a try catch. Two, on
close set a boolean variable to tells you it has been disposed.

Try
frm.Show()
Catch ex As System.ObjectDisposedException
frm = New FormName
frm.Show()
Catch ex As Exception
MsgBox(ex.message)
End Try
 
Back
Top