Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cleaning objects off a form, Where and When?

I have a simple Windows form application. On the form I have a custom class that has it's own Dispose method.

So the question is when should I call this?

Is the FormClosed event (i.e. Form1_FormClosed) the correct place to do this? Or should I be writing a custom Dispose method for the form?

For bonus points: Can a from be reopend once closed? (Obviously if it can then the FormClosed is the wrong way to go!)

Thanks.

like image 727
AidanO Avatar asked Dec 29 '22 02:12

AidanO


1 Answers

A wee bit of surgery is required. Open the node next to your form in the Solution Explorer window. Double-click the Designer.cs file for the form. Locate the Dispose() method and cut-and-paste it into your form's source code file. Now you can alter it and call the Dispose methods on the disposable object references in your form class.

Pre-empting: no, it is okay to edit this part of the designer file. Only the section in the #region is off limits.

like image 160
Hans Passant Avatar answered Jan 09 '23 15:01

Hans Passant