Using VB Express 2010 in windows XP Pro 64.

A ListBox shows the project forms names.
On ListBox_DoubleClick

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?

Exactly. Close() method calls a Dispose() method, which terminates and removes the instance of an object. This means that it does no exist any longer.
To use a Show() method again, you have to create a new instance of an object.

Alternative: Instead of calling Close or Dispose methods, better call Hide() method, which will only hide an object (so it will no be vissible), but when you will call Show() method again, this object will pop up again.

Thanks Geek and Mitja.

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 :

e.Cancel = True
Me.Hide()

on the closing event but, how come a button click event xf.show() doesnt have the object disposed issue.

how come a button click event xf.show() doesnt have the object disposed issue.

Thats a kinda weird question. Why should the "Show" function dispose an object? That would make no sense at all. Maybe you should check out what "Dispose" means.

Geek, you said:

After closing a Form it does not exist anymore.
Create a new instance of the form.

But after closing XForm2 and clicking a button on the first form with the following in the click event

XForm2.Show

It does show it. So how come this show method still works and not the same method used while looping through the collection? I'm certain that the collection wasn't disposed. So, what is the difference? Thank you.

This article has been dead for over six months. Start a new discussion instead.