Results 1 to 17 of 17

Thread: Not reopening the same form

  1. #1

    Thread Starter
    Hyperactive Member The Dutch Dude's Avatar
    Join Date
    Nov 2001
    Location
    Holland. Land of the Dutch. Home for me.
    Posts
    261

    Not reopening the same form

    A friend of mine has a problem in VB.NET, and since I'm no expert on the .NET variant, I thought I'd ask it here on his behalf.

    His problem is the following: He has a button which opens a form, but pressing the button several times in order opens the same form over and over again. Is there a way to put a stop to this??
    Obey the dragon thing. Or not. Or possibly just a bit.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Umm , you mean it open multiple instances of the form ? and he wants to open only one form ?

  3. #3

    Thread Starter
    Hyperactive Member The Dutch Dude's Avatar
    Join Date
    Nov 2001
    Location
    Holland. Land of the Dutch. Home for me.
    Posts
    261
    that's the general idea. I just called him and he said he was using a MDI interface. There's a menu-item that opens the child window and he can either create a new instance of the form in that button's code (causing said problem) or publicly declare the form once an have it destroyed by .NET Garbage collecting code titbits. So, there it is...

    *edit: woopsie... the problem is that in the first case he can't stop the opening of multiple instances and in the second case he can only display the window once, after which garbage stuff kicks in and the instance is destroyefied
    Last edited by The Dutch Dude; Sep 8th, 2003 at 02:11 PM.
    Obey the dragon thing. Or not. Or possibly just a bit.

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    So what's the problem now ?

  5. #5
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374
    Has anyone found a solution to this problem?

    In .NET, it seems that you get multiple instances of the same form if you declare the form locally in a button click procedure.

    The solution would seem to be to declare the form at form level rather than locally so that when you call Form1.Show, an existing instance of the form is displayed rather than creating a second or third instance of the same form.

    However, once you close the form, the garbage collector disposes of it and you get an error next time you call Form1.Show

    I have tried declaring the form at form level (Dim Form1 As Form1) and putting this in the button click procedure:

    VB Code:
    1. If IsNothing(Form1) = False Then
    2.  
    3.             Form1.Dispose()
    4.             Form1= Nothing
    5.             Form1 = New Form1
    6.  
    7.         End If
    8.  
    9.        
    10.         Form1.MdiParent = Me
    11.         Form1.Show()

    Although this prevents multiple instances of the same form being created and allows you to re-open the same form, it you have an existing instance of Form1, it will be replaced with a fresh instance. I want to be able to re-activate an existing instance.

    I have also tried:

    VB Code:
    1. If IsNothing(Form1) = False Then
    2.  
    3.            Form1.Activate()
    4.  
    5.         Else
    6.  
    7.             Form1= New Form1
    8.             Form1.MdiParent = Me
    9.             Form1.Show()
    10.  
    11.         End If

    The problem with this is that once Form1 has been opened and closed, IsNothing(Form1) returns False. It only returns True prior to creating the first instance.

    This was so straightforward in VB6.

  6. #6
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    Then why are you distroying the instance?

    VB Code:
    1. 'Just use Me.Hide when you want the form to "hide"
    2. Form1.Hide

    and when you call Form1.Show it will come back with all the properties and controls just as they were before.
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  7. #7
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Isn't the simplest thing to do as follows:

    On opening the form, set the button.enabled property to false.

    In the form.closed event set the button.enabled property to true.

    Or use the visible property if preferred.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  8. #8
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374
    <ABX,

    I am destroying the instance of the form because the instance seems to exist even after the form is closed.

    ie. IsNothing(Form1) returns False after closing the form.

    I have even tried putting Me.Dispose in the Form's Closing event in the hope that that would make future calls to IsNothing(Form1) return True.

    I want to be able to detect whether there is an active instance of the form (hidden by another form) and when the button that shows the form is clicked:

    either

    1. An existing instance is activated

    or

    2. A new instance is created if there is no existing instance

    Thanks for the suggestion, taxes, but I want the button to be enabled so that an existing instance can be activated.

  9. #9
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi

    "I want to be able to detect whether there is an active instance of the form (hidden by another form) and when the button that shows the form is clicked:

    either

    1. An existing instance is activated

    or

    2. A new instance is created if there is no existing instance

    Thanks for the suggestion, taxes, but I want the button to be enabled so that an existing instance can be activated."


    Come on, you're not thinking this thing through.

    I understand you to say that you only want ONE instance of the form, but that it might be hidden.

    Therefore, in the click event test to see what the text is. Assuming the original text of the button is "Create Form1",
    if it is still "Create Form1" then create the instance and change the text to "Show Form1".

    If the text is "Show Form1" and then show form1.

    In the closed event of form1, change the text of the button to "Create Form1".
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  10. #10
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    To stop multi-instantiation , this one trick :

    VB Code:
    1. 'This class member
    2. Private i As Integer = 0
    3.  
    4. 'This goes in a button
    5. If Not i = 1 Then
    6.             Dim frmChild As New Form2
    7.             frmChild.Show()
    8.             i = 1
    9.         End If

  11. #11
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374
    I have still got the problem of detecting whether an instance of the form object exists.

    Why does IsNothing(Form1) return False after the Form1 has been closed?

    Shouldn't closing Form1 set the Form1 object to nothing?

    How do I explicitly set the form object to nothing when it is closed?

    In VB6, it was
    VB Code:
    1. Unload Form1
    2. Form1.Hide
    3. Set Form1 = Nothing
    The "Set" keyword is no longer supported but I can only call
    Form1 = Nothing in the form class where Form1 is declared (at form level). I want to be able to set it to nothing in the closing event of the form that is being closed.

  12. #12
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    "I have still got the problem of detecting whether an instance of the form object exists."

    If you use my suggestion, you will know that an instance of the form exists by checking the text of the button!!!!!!
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  13. #13
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    When you close your form , it's disposed but not immediately . It's marked as orphan object and collected when the Garbage Collector is fired . Sadly , you can't guess when GC is fired . It's working randomizly under some vague rules .

  14. #14
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    Well, just do a SingleTon class
    \m/\m/

  15. #15
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I would do it this way , if I were you :

    MainForm :

    VB Code:
    1. 'Class member
    2. Dim frmchild As New childfrm
    3. Dim i As Integer = 0
    4.  
    5. Private Sub Button1_Click(ByVal sender As System.Object, ByVal
    6. e As System.EventArgs) Handles Button1.Click
    7.         If i = 1 Then
    8.             frmchild.ActivateMe()                        
    9.         Else
    10.             frmchild.Show()            
    11.             i = 1
    12.         End If
    13. End Sub


    and in the ChildForm (second form you're showing) , do this :
    Add sub with Public modifier , something similar to this :

    VB Code:
    1. Public Sub ActivateMe()
    2.         Me.Focus()
    3.     End Sub

    This would create the form if not existed , if it's already there , then it's would get focused .

  16. #16

  17. #17
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374
    Thanks for your suggesions, guys.

    Edneeis, I used this from the first thread and it worked:

    VB Code:
    1. Public Sub ShowSingleInstance(ByVal childType As Type, ByVal args() As Object)
    2.         For Each child As Form In Me.MdiChildren
    3.             If child.GetType Is childType Then
    4.                 'already loaded so bring to foreground
    5.                 child.Activate()
    6.                 Return
    7.             End If
    8.         Next
    9.         'not loaded yet so create
    10.         Dim frm As Form = Activator.CreateInstance(childType, args)
    11.         frm.MdiParent = Me
    12.         frm.Show()
    13.     End Sub
    14.  
    15.     Public Sub ShowSingleInstance(ByVal childType As Type)
    16.         ShowSingleInstance(childType, Nothing)
    17.     End Sub
    18.  
    19. 'syntax for no args
    20. ShowSingleInstance(GetType(frmChildReplace))

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