locked
Open Disposed Objects RRS feed

  • Question

  • How do I re-open a form that has been disposed?  I tried using GC.Collect() which did not work.

    I have 2 forms.  The first form being the main form and the second being the secondary form.  I open the secondary form with form one.  Form 2 two opens and after I am done with it I close it.  Now I want to open up the second form again and BAME!  Error.  Cannot access a disposed object.

    I assume that the answer to this is a really simple answer but I have always had problems with this error through most of my programs that I have created.  Sadly now probably 2 or 3 years later is when I am asking for the solution.

    Wednesday, October 5, 2011 12:51 AM

Answers

  • The answer is really simple.  You cannot use the disposed instance.  You need to creat a new instance of the form.  Alternatively you could not call Dispose() on the form when you finished using it.
     

    --
    Mike
    • Proposed as answer by Armin Zingler Wednesday, October 5, 2011 10:30 AM
    • Marked as answer by Metroidn1f Friday, October 7, 2011 12:24 AM
    Wednesday, October 5, 2011 12:55 AM

All replies

  • The answer is really simple.  You cannot use the disposed instance.  You need to creat a new instance of the form.  Alternatively you could not call Dispose() on the form when you finished using it.
     

    --
    Mike
    • Proposed as answer by Armin Zingler Wednesday, October 5, 2011 10:30 AM
    • Marked as answer by Metroidn1f Friday, October 7, 2011 12:24 AM
    Wednesday, October 5, 2011 12:55 AM
  • I never even tried calling Me.Dispose() on the second form, but wouldn't it work if I did do it on FormClosed or FormClosing?

    Are you saying that if you could call do Me.Dispose() on the form before it closes it would be possible to re-open it?

    Wednesday, October 5, 2011 1:55 AM
  • No, the opposite is the case. You can not open it again because Dispose has been called. Dispose is called automatically if you close a modeless Form. The answer is what Mike said: Create a new instance.

    Another way is not to close the Form but instead hide it only. Then you can just show it again the next time you need it. You can hide it in the FormClosing event and set e.Cancel = True. But be aware that you must do this conditionally only because the application must still be closable. If the Form is always hidden only and cannot be closed at all, it is always running.


    Armin
    Wednesday, October 5, 2011 10:29 AM
  • Alright, thank you.
    Friday, October 7, 2011 12:23 AM