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

Prevent more than one instance from loading.

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
--------------------
"Michael" <Mi*****@discussions.microsoft.com> wrote in message
news:8A**********************************@microsof t.com...
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

Nov 21 '05 #2

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

"Ken Tucker [MVP]" wrote:
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
--------------------
"Michael" <Mi*****@discussions.microsoft.com> wrote in message
news:8A**********************************@microsof t.com...
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


Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.