.NET 2
// dynamic textbox adding
myTextBox = new TextBox();
this.Controls.Add(myTextBox);
// ... some code, finally
// dynamic textbox removing
myTextBox.Dispose();
// this.Controls.Remove(myTextBox); ?? is this needed
Little explanation
- Surely, if I Dispose a control I will not see it anymore, but anyway, will remain a “Nothing” in the parent controls collection?
- need I also, like MSDN recommends, remove all handlers from the control?
No, you don’t.
I tried it.
You can paste the following code into LINQPad:
EDIT: The control will be removed from the form’s
Controlscollection. To demonstrate this, replace the click handler with the following:It will show
0.2nd EDIT:
Control.Dispose(bool disposing)contains the following code: