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 Controls / December 2006

Tip: Looking for answers? Try searching our database.

run-time adding/removing controls in a panel

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Magallo - 04 Dec 2006 15:43 GMT
Hi all!

I've got a performance problem. In my application, I need to add/remove some
user controls at run-time inside a panel. My problem is that I can't use the
foreach statement to enumerate the controls to remove them.

For example: if I use this code, it throws me an exception:

foreach( Control c in mypanel.Controls )
{
  myPanel.Controls.Remove(c);
}

The exception is raised because I try to modify the collection
myPanel.Controls that is used in the foreach statement. So, I solved this in
this way, that is using a for statement with an index starting from the end
to the start of the collection.

for( int i = myPanel.Controls.Count - 1; i >= 0; i-- )
{
  myPanel.Controls.Remove(myPanel.Controls[i]);
}

This way works. My question is: is this the right way to do or there is some
better ways?

Another problem: performace. Doing so, everything works, but it's really
slow and looks bad because you can see the controls disappearing slowly one
by one. What I'd like to do is lock the panel UI, remove all the controls
that need to bo removed, and then unlock the panel UI to show it refreshed
at the end of the removing process. I think that if I am able to do this, I
will get a great performance improvement and also a good looking. I tried
the SuspendLayout()/ResumeLayout() calls but they didn't change anything at
all. Any idea?

Thanks.

Matteo Galletti.
Jeff Gaines - 04 Dec 2006 16:07 GMT
On 04/12/2006 in message <uyn9xr7FHHA.4588@TK2MSFTNGP05.phx.gbl> Magallo
wrote:

>The exception is raised because I try to modify the collection
>myPanel.Controls that is used in the foreach statement. So, I solved this
[quoted text clipped - 8 lines]
>This way works. My question is: is this the right way to do or there is
>some better ways?

I usually do it in 2 stages. First go through and add the controls to an
ArrayList then go through the ArrayList and delete the controls there. It
may be worth trying so you can compare the speed?

Signature

Jeff Gaines


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.