Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to reuse a .NET WinForms Form object?

Tags:

.net

winforms

Once a window has been closed, it is possible, and it is recommended, to reuse that window's Form instance to show the same window again? Or is it required or recommended to always create a brand new instance of the class when you will be showing a window.

What this really boils down to is whether it is a good idea to ever call Show() or ShowDialog() more than once on the same object, as long as the window is closed in between.

If this is not recommended, an explanation of the underlying reasons would also be appreciated.

like image 407
Chris Ammerman Avatar asked Jan 13 '09 17:01

Chris Ammerman


1 Answers

No, and no.

A call to Close ends up calling Dispose, and the object is considered disposed.

There is no problem in hiding the form and then showing it again, but closing it is a definite no-no, since the state is undefined after it is disposed (well, the state is disposed, but using it is the same as using something that is undefined).

like image 112
casperOne Avatar answered Oct 11 '22 16:10

casperOne