Being a newby in .NET land and coming from a Delphi background, I am accustomed to create controls dynamically and FREE them manually when needed.
In .net, being garbage collected and so, I guess you don’t need to explicitly free the controls.
In my case (WinForms), I dynamically populating a flowLayoutPanel with panels, that contains some other controls. In some cases, I need to remove some panels. What I’m doing to achieve that is
flowPanel.Controls.Remove(thePanelToRemove);
This have the needed effect: the panel disappears from the flowLayoutPanel, but what I don’t get is: does the garbage collector deletes the control? That would be the desired behavior because I will be creating lots of controls that won’t be used anymore when the user deletes them. If not, how can I be sure that the controls get freed? Something like (pseudocode)
flowPanel.Controls.Remove(thePanelToRemove);
thePanelToRemove.Free();
or similar?
Short answer: yes, the garbage collector will remove the control when memory is needed.
Longer answer: Some controls claim resources that can’t be freed by the garbage collector. Those controls implement the IDisposable interface and you should call dispose on the control when you no longer need it. The Dispose will clean up these unmanaged resources.