Results 1 to 10 of 10

Thread: How to not duplicate a form?

  1. #1

    Thread Starter
    Addicted Member AlvaroF1's Avatar
    Join Date
    Sep 2002
    Location
    SP - Brazil
    Posts
    200

    Question How to not duplicate a form?

    I'm using the following code to show a form:

    VB Code:
    1. Dim frm As New Form1()
    2. frm.Show()
    How to not duplicate this form? Instead, I'd like to show the opened one.


    Thanks.

  2. #2
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536
    Make frm a global variable and use frm.Visible = True/False.
    This world is not my home. I'm just passing through.

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    This allows only one instance of the current form :
    VB Code:
    1. 'allows one instance of the form named Form1
    2.         Static counter As Integer = 0
    3.         If counter = 0 Then
    4.             Dim frm As New Form1
    5.             frm.Show()
    6.             counter += 1
    7.         Else
    8.             Exit Sub
    9.         End If

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You can also use this:
    VB Code:
    1. Static frm As Form1
    2. If frm Is Nothing Then
    3.   frm=New Form1() 'create
    4. Else
    5.    frm.Activate() 'bring to front
    6. End If

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 .

  6. #6
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536
    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:
    1. Shared InstanceCount As Integer
    2.  
    3. Sub New()
    4.     If InstanceCount > 0 Then
    5.         Me.Show
    6.     Else
    7.         InstanceCount += 1
    8.         MyBase.New
    9.     Endif
    10. End Sub
    This world is not my home. I'm just passing through.

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Static frm As Form4
    3.         If frm Is Nothing Then
    4.             frm = New Form4 'create
    5.             frm.Show()
    6.         Else
    7.             frm.Activate() 'bring to front
    8.         End If
    9.     End Sub

  8. #8
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Correction

    Should have used Exit Sub, like this...

    VB Code:
    1. Shared InstanceCount As Integer
    2.  
    3. Sub New()
    4.     If InstanceCount > 0 Then
    5.         Exit Sub
    6.     Else
    7.         InstanceCount += 1
    8.         MyBase.New
    9.     Endif
    10. End Sub
    This world is not my home. I'm just passing through.

  9. #9

    Thread Starter
    Addicted Member AlvaroF1's Avatar
    Join Date
    Sep 2002
    Location
    SP - Brazil
    Posts
    200
    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Static frm As Form4
    3.         If frm Is Nothing Then
    4.             frm = New Form4 'create
    5.             frm.Show()
    6.         Else
    7.             frm.Activate() 'bring to front
    8.         End If
    9.     End Sub
    Good one, Edneeis, but how to set "frm" to "Nothing" when this form close?

    Thanks.

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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
  •  



Click Here to Expand Forum to Full Width