Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / February 2007

Tip: Looking for answers? Try searching our database.

Dispose questions

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JS - 13 Feb 2007 13:34 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.