|
-
Sep 21st, 2003, 09:50 PM
#1
Thread Starter
Addicted Member
How to not duplicate a form?
I'm using the following code to show a form:
VB Code:
Dim frm As New Form1()
frm.Show()
How to not duplicate this form? Instead, I'd like to show the opened one.
Thanks.
-
Sep 22nd, 2003, 08:31 AM
#2
Make frm a global variable and use frm.Visible = True/False.
This world is not my home. I'm just passing through.
-
Sep 22nd, 2003, 09:37 AM
#3
Sleep mode
This allows only one instance of the current form :
VB Code:
'allows one instance of the form named Form1
Static counter As Integer = 0
If counter = 0 Then
Dim frm As New Form1
frm.Show()
counter += 1
Else
Exit Sub
End If
-
Sep 22nd, 2003, 10:02 AM
#4
You can also use this:
VB Code:
Static frm As Form1
If frm Is Nothing Then
frm=New Form1() 'create
Else
frm.Activate() 'bring to front
End If
-
Sep 22nd, 2003, 10:07 AM
#5
Sleep mode
I've been trying the same code Edneeis for many times but still generating new instances . Use Show Method and see how many instances you'll get .
-
Sep 22nd, 2003, 10:16 AM
#6
How about using a shared member in the form? I can't try this out right now, but it should work. One of the problems with using a static (instead of a global) is that instances of the form can be created from other modules or forms.
VB Code:
Shared InstanceCount As Integer
Sub New()
If InstanceCount > 0 Then
Me.Show
Else
InstanceCount += 1
MyBase.New
Endif
End Sub
This world is not my home. I'm just passing through.
-
Sep 22nd, 2003, 10:22 AM
#7
Yeah I forgot the show line, but it worked fine for me. You have to make sure you you call that same procedure everytime to start the form though.
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Static frm As Form4
If frm Is Nothing Then
frm = New Form4 'create
frm.Show()
Else
frm.Activate() 'bring to front
End If
End Sub
-
Sep 22nd, 2003, 10:23 AM
#8
Correction
Should have used Exit Sub, like this...
VB Code:
Shared InstanceCount As Integer
Sub New()
If InstanceCount > 0 Then
Exit Sub
Else
InstanceCount += 1
MyBase.New
Endif
End Sub
This world is not my home. I'm just passing through.
-
Sep 23rd, 2003, 12:22 PM
#9
Thread Starter
Addicted Member
Originally posted by Edneeis
Yeah I forgot the show line, but it worked fine for me. You have to make sure you you call that same procedure everytime to start the form though.
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Static frm As Form4
If frm Is Nothing Then
frm = New Form4 'create
frm.Show()
Else
frm.Activate() 'bring to front
End If
End Sub
Good one, Edneeis, but how to set "frm" to "Nothing" when this form close?
Thanks.
-
Sep 23rd, 2003, 12:30 PM
#10
You don't, you just let the GarbageCollector do its thing and take out the garbage. You can still catch any events you need on the form if you have to dispose of anything manually.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|