Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disposing dynamically created controls

I have a WinForms TabControl that I am dynamically adding TabPages to at runtime. Each TabPage contains a WebBrowser control. I also have the ability to remove the TabPages at runtime.

Should I bother Dispose()ing the TabPage and/or WebBrowser controls?

It seems to me I should at least Dispose() the WebBrowser control since it is a bit of a resource hog.


1 Answers

You should Dispose() your tab page when you remove it. This will automatically dispose all of the child controls.

For details, see the Control.Dispose documentation:

Releases the unmanaged resources used by the Control and its child controls and optionally releases the managed resources.

The tab page's dispose will also dispose of all of the children controls for you.

like image 93
Reed Copsey Avatar answered Aug 03 '26 18:08

Reed Copsey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!