Prevent more than one instance from loading.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi Everyone,
I'm looking for a way to make sure that only one instance of a form is
loaded. I have tried the following:
Private Sub mnuPatients_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuPatients.Click
try
if not ifrmPatients is nothing then
exit Sub
End If
cursor = cursors.WaitCursor
ifrmPatients = new frmPatients
ifrmPatients.MdiParent = me
ifrmPatients.Show
cursor = cursors.Default
Catch ex As Exception
msgbox(ex.Message )
End try
End Sub

That didn't seem to work by itself. In the Closed event of the child forms
I even tried to force a Dispose, but that don't seem to get rid of the
reference. ANy ideas.
THanks.
Michael
 
Hi,

I usually add a definstance property of a form if I only want
one instance of the form. I would call Form1.DefInstance.Show to display
the form.


Add to the forms new procedure

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call
frm = Me
End Sub

Add to the forms dispose procedure

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
frm = Nothing
MyBase.Dispose(disposing)
End Sub


Add the following code to the form

Private Shared frm As Form1

Public Shared ReadOnly Property DefInstance() As Form1
Get
If frm Is Nothing Then frm = New Form1
Return frm
End Get
End Property

Ken
 
Hi Ken,
Thank you so much for your help. That did the job. I found other tips to do
the same thing, but could not get them to work right. But this one did it.
Thanks again.
Michael
 

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

Back
Top