how to prevent multiple instances of 2nd form?

G

Guest

Hello,

my app opens a 2nd form (form2) by clicking a button on the first form
(form1). I do not want to open form2 in modal form, but I only want one
instance of form2 open. So if someone re-clicks the button on form1 there
will not be multiple instances of form2. I created a module level boolean
var in form1 and a property in form1 to control the loading of form2, but I
am having a problem resetting the value of this boolean var. Here is what I
have:

'--in form1 I have this property

Public Property frmLoaded() As Boolean
Get
Return bFrmLoaded
End Get
Set(ByVal Value As Boolean)
bFrmLoaded = Value
End Set
End Property

Sub...
If bFrmLoaded.Equals(False) Then
form2.Show
bFrmLoaded = True
End If
....
End Sub

In form2 in the closing event I want to reset the value of bFrmLoaded to
False. But when I set a form var to form1 - it is a new instance of the form
Dim frm As New form1
frm.frmLoaded = False

The original form1 does not receive this. I can't remember how I dealt with
this in the past. I have done this before. Just can't remember what I did.
Or is there a better approach to dealing with keeping only one instance of a
form (which is not in modal form or mdi, etc)?

Any suggestions appreciated on how to reset the form1 bFrmLoaded boolean
var from form2.

Thanks,
Rich
 
C

Cyril Gupta

Hmm...

Well your strategy is not going to work, because very act of accessing the
variable will create a new instance of the form.

This is what you should do, and it's damn easy :)

Dim frm as New Form1

If frm Is Nothing Then
Frm.show
End If

Regards
Cyril Gupta
 
G

Guest

OK. I found this code snippet that seems to work for my purposes:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
' If the instance still exists... (ie. it's Not Nothing)
If Not IsNothing(F2) Then
' and if it hasn't been disposed yet
If Not F2.IsDisposed Then
' then it must already be instantiated - maybe it's
' minimized or hidden behind other forms ?
F2.WindowState = FormWindowState.Normal ' Optional
F2.BringToFront() ' Optional
Else
' else it has already been disposed, so you can
' instantiate a new form and show it
F2 = New Form2
F2.Show()
End If
Else
' else the form = nothing, so you can safely
' instantiate a new form and show it
F2 = New Form2
F2.Show()
End If

End Sub

But if anyone has any other or additional suggestions - please share.

Thanks
 
C

Cyril Gupta

Hello Rich,

I think it is a perfectly valid solution.

Note that it combines what Cor and I suggested :)

If you check for IsDisposed without checking for Is Nothing first, you will
get an error, or will create an instance of the form.

Regards
Cyril Gupta
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top