By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,578 Members | 1,835 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,578 IT Pros & Developers. It's quick & easy.

Avoid opening a second instance of a form

P: n/a
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!

--
Lasse
Nov 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
Nov 20 '05 #2

P: n/a
Hello,

"Lasse Eskildsen" <Le****@DELETEwebspeed.dk> schrieb:
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".

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #3

P: n/a
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
"Lasse Eskildsen" <Le****@DELETEwebspeed.dk> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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!

--
Lasse

Nov 20 '05 #4

P: n/a
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
Nov 20 '05 #5

P: n/a
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!

--

Lasse

"Lasse Eskildsen" <Le****@DELETEwebspeed.dk> skrev i en meddelelse
news:%2****************@TK2MSFTNGP09.phx.gbl...
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!

--
Lasse

Nov 20 '05 #6

P: n/a
"Lasse Eskildsen" <Le****@DELETEwebspeed.dk> schrieb
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.
--
Armin

Nov 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.