Results 1 to 8 of 8

Thread: Form.Show() wont work after closed

  1. #1

    Thread Starter
    New Member At--o's Avatar
    Join Date
    Aug 2011
    Posts
    9

    Thumbs up Form.Show() wont work after closed

    Using VB Express 2010 in windows XP Pro 64.

    A ListBox shows the project forms names.
    On ListBox_DoubleClick

    Code:
        For Each F As Form In xFormCollection
        If F.Name = ListBox.SelectedItem.ToString Then
        Try
        F.Show()
        Catch ex As Exception
        MsgBox(ex.Message)
         
        End Try
         
        End If
        Next



    Works fine the first time but after closing the form opened by the DC, if I try to open it again, throws an exception "Cannot access a disposed object".

    Since I can still access any other form not previously showed I guess that the closing changes a status that allowed the form to be .Show, there is
    no F.Load , F.Activate after the exception doesn't seem to do anything. What should I do?

  2. #2

  3. #3
    Member
    Join Date
    Sep 2010
    Posts
    41

    Re: Form.Show() wont work after closed

    Quote Originally Posted by boops boops View Post
    Hide the form instead of closing it. BB
    To elaborate on BB's answer...

    Yes, you can hide it instead of closing it. However, I would still close it, but instead of using the default instance, I'd create a new instance of the form each time.

    The reason you're getting the exception is the exact reason the exception explained. You opened object and then you got rid of it (closing = disposed). Once that object's been disposed, it's gone.

    But if you create a new instance each time, then the next time you open that form, it will be a different instance from the one you closed. So something like this should work.

    VB.NET Code:
    1. For Each F As Form In xFormCollection
    2.     If F.Name = ListBox.SelectedItem.ToString Then
    3.     Try
    4.     F = New Form
    5.     F.Show()
    6.     Catch ex As Exception
    7.     MsgBox(ex.Message)
    8.      
    9.     End Try
    10.      
    11.     End If
    12.     Next

  4. #4

    Thread Starter
    New Member At--o's Avatar
    Join Date
    Aug 2011
    Posts
    9

    Re: Form.Show() wont work after closed

    Thanks boops boops and JhonDorian.
    A new instance of a Form wouldnt have the controls and functionality. Couldnt create as New F since F is not a class and if as XF AS New Form = F then the same dispose occurs in F by closing XF. So, for now Im using :
    Code:
    e.Cancel = True
    Me.Hide()
    on the closing event but, how come a button click event xf.show() doesnt have the object disposed issue.

  5. #5
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Form.Show() wont work after closed

    Your xFormCollection should either get updated when a form is closed (thus, removed from the collection) or you should prevent the form from being closed.

    Quote Originally Posted by At--o View Post
    how come a button click event xf.show() doesnt have the object disposed issue.
    Can you elablorate?

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Form.Show() wont work after closed

    There is definitely some confusion going on here. I agree with the ideas posted here, but they appear not to work, so there is some part of this puzzle that we have not seen, yet. Creating a new instance is what makes most sense to me, unless you are doing things with the current instance which you want to retain. For instance, if the current instance has data displayed in a certain fashion, you would want to hide/show that, since closing will get rid of the instance. Creating a new instance is problematic, though, since you appear to have a collection of forms. Form is the base class that all the forms in the collection have derived from. You can't create a new form and expect it to be useful, as you have noted. Instead, you would need to create a new instance of the proper form type, which you may not even know, unless your collection is all different instances of the same type of form.

    As for why this appears not to have the issue in the button click, I would say that it is because you are showing two different instances in the different places, without realizing it. Showing us the code that appears not to have that issue might be sufficient to clear it up....but it might not, too.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    New Member At--o's Avatar
    Join Date
    Aug 2011
    Posts
    9

    Re: Form.Show() wont work after closed

    Code with no issue

    Code:
    Public Class frmMain
        Inherits aaMyForm
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles        Button1.Click
            frm2.Show()
    End Sub
    
    End Class

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Form.Show() wont work after closed

    Yeah, that could be an issue. Frm2 looks an awful lot like a default instance, and those are evil because of the confusion they cause...combined with the fact that they are utterly unnecessary.

    The default instance is a single instance of each type of form in your project that is quietly made available to you when the program starts. You are using the default instance if you didn't specifically create an instance using New. If that is the case, get away from using the default instances. They mostly create confusion.

    By the way, as a member of the class of AT9, I like your avatar. What does the user name mean?
    My usual boring signature: Nothing

Tags for this Thread

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