Dispose questions

J

JS

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top