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
                    Catch ex As Exception
                    End Try

                End If

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?

6 Years
Discussion Span
Last Post by AT--O

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

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


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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.