Checking for existing instance of a form

  • Thread starter Thread starter Ivan Weiss
  • Start date Start date
I

Ivan Weiss

I am very accustomed to VB6 and therefore the OOP characteristics of
VB.Net are giving me some difficulties. How can I ensure that only one
instance of a form is created (I am using MDI Parent and Child forms).
I only want the user to be able to have one window open at a time within
the MDI Client. I thought the easy way would be to disable the toolbar
used to call the other forms but have not figured out that yet. Is
there a better way to ensure only one instance is created?

-Ivan
 
Hi Ivan,

Here's something to give you ideas.

Regards,
Fergus

<code status="untested">
Private Sub MnuFooBar_Click(....) Handles MnuFooBar.Click
CreateOrActivateForm ("FooBar!!", GetType (frmFooBar))
End Sub

Private Sub CreateOrActivateForm _
(sHeading As String, oFormType As Type)
Dim oForm As Form
'Exit if we already have this form...
For Each oForm In Me.MdiChildren
If oForm.Name = sHeading Then
oForm.Activate
Return
End If
Next

' Not found ... Let's create it.
oForm = New Activator.CreateInstance (oFormType)
oForm.MdiParent = Me
oForm.Show()
End Sub
</code>
 
* Ivan Weiss said:
I am very accustomed to VB6 and therefore the OOP characteristics of
VB.Net are giving me some difficulties. How can I ensure that only one
instance of a form is created (I am using MDI Parent and Child forms).
I only want the user to be able to have one window open at a time within
the MDI Client. I thought the easy way would be to disable the toolbar
used to call the other forms but have not figured out that yet. Is
there a better way to ensure only one instance is created?

<http://groups.google.de/[email protected]>

As an alternative your child form can implement the Singleton design
pattern.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Dilbert's words of wisdom #18: Never argue with an idiot. They drag you down
to their level then beat you with experience.
 
Back
Top