Avoid opening a second instance of a form

  • Thread starter Thread starter Lasse Eskildsen
  • Start date Start date
L

Lasse Eskildsen

Hi,

How can I make sure I don't show the same form twice? If the form is already
shown, just focus. I tried using ownedforms, but can't quite make it work...

Thanks in advance!
 
Hi Lasse,

One way is to have a class which your Forms register with when they open
and close. Then, if there's a request for a Form that is already showing it
can be activated and brought to the front.

I answered a question about switching Forms (Topic: Main Menu, 13th Sep).
There's a class given (and a project attached) that may give you some ideas.
It's about closing and opening Forms, but I think it may be adapted to suit
your needs.

Add another question here if you need further help. :-)

Regards,
Fergus
 
Hello,

Lasse Eskildsen said:
How can I make sure I don't show the same form twice?
If the form is already shown, just focus. I tried using
ownedforms, but can't quite make it work...

Have a Google Search on the "singleton design pattern".
 
Hi Lasse

I use the following function in an MDI application to avoid opening the same
MDI child form twice:

<code>
Private Function IsFormOpen(ByVal Title As String, ByVal ActivateOnFound As
Boolean) As Boolean

Dim frm As System.Windows.Forms.Form
Dim IsOpen As Boolean = False

For Each frm In Me.MdiChildren
If frm.Text = Title Then
' It is open
If ActivateOnFound Then
frm.Activate
End If

IsOpen = True

Exit For
End If
Next frm

Return IsOpen

End Function
</code>

It assumes that you know the title/caption of the form that you are trying
to open.

Called like this:

<code>
If Not IsOpen("Some Form", True) Then
' Create new instance of "Some Form" and open it
End If
</code>

HTH

Charles
 
Hi Lasse, Charles,

My suggestion is for the non-MDI situation. Essentially you have to
implement the effect of Charles' code, but you also have to maintain your own
version of MdiChildren.

Regards,
Fergus
 
Lasse Eskildsen said:
How can I make sure I don't show the same form twice? If the form is
already shown, just focus. I tried using ownedforms, but can't quite
make it work...

Store the reference to the Form. Next time, you'll know that it's already
shown by comparing the reference to Nothing. When the Form is closed, set
the reference to Nothing.
 
Hi,
Thanks for the help to everybody. I use both mdi and non-mdi from my
mainform, so with some help from you guys, I wrote the function below:
Private Function FormIsOpen(ByVal Titel As String, ByVal ActivateOnFound As
Boolean) As Boolean

Dim frm As System.Windows.Forms.Form

Dim IsOpen As Boolean = False

For Each frm In Me.MdiChildren

If frm.Text = Titel Then 'The form is open

If ActivateOnFound Then

frm.Activate()

End If

IsOpen = True

End If

Next

For Each frm In Me.OwnedForms

If frm.Text = Titel Then 'The form is open

If ActivateOnFound Then

frm.Activate()

End If

IsOpen = True

End If

Next

Return IsOpen

End Function

And use me.addownedform() for non-mdi forms.Thanks for the help!
 
Back
Top