I have a user control with a panel on it. This panel is dynamically
populated with controls and sometimes I need to clear off all of the
controls and repopulate with new ones.
1. Disposing all controls on the panel
for (int ii=this.panel.Controls.Count-1 ; ii>=0 ; ii--)
this.panel.Controls[ii].Dispose();
this.panel.Controls.Clear();
Q: is this the best way to make sure all of these controls get
disposed? I noticed that using foreach() will not go through all the
controls (probably because Dispose() also removes the control from the
collection), and it does not generate an exception about the
collection being changed either!
2. Changing fonts
Each of my child controls needs to use the same font. Can I set it
like this:
foreach (Control c in this.panel.Controls)
c.Font = this.childControlFont;
Will the control dispose the font when it is done? What is the best
way for multiple controls to share a font?
Are there any best practices documents/websites that discuss using and
disposing controls and gdi objects?
Thanks.
Kevin Spencer - 14 Feb 2007 11:58 GMT
I think you're on the right track.

Signature
HTH,
Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com
The shortest distance between 2 points is a curve.
>I have a user control with a panel on it. This panel is dynamically
> populated with controls and sometimes I need to clear off all of the
[quoted text clipped - 26 lines]
>
> Thanks.