Difference in Dispose Behavior Between a Form and Windows Forms Text Fields

Whenever I call the Dispose method on a Windows Forms form (obtained from System.Windows.Forms.Form) to close it, the Dispose method ends up freeing up resources and deleting the form.

I have runtime objects such as text fields, as shown below:

 Textbox Tb = new Textbox(); 

The user can create new text fields dynamically. I want the text fields containing the data to remain around, and those that are empty are deleted. When I call the Dispose method on empty text fields, at runtime it looks as if they are located, but usually they are simply invisible.

So:

  • What is the difference between calling the dispose method in text blocks and classes derived from forms?

  • Why is the form located during the call, for example, Form1.Dispose(); and why not text fields at runtime as shown below?

     if (tb.text=="") tb.Dispose(); 
+4
source share
1 answer

From what I understand, this is due to ownership. The form owns the controls, so if you manage the controls, well, you just need to update them. If you dispose of the form itself, it is gone, nothing was updated.

+1
source

All Articles