[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Problem with clearing controls in application

Posted on 2008-02-08
5
Medium Priority
?
212 Views
Last Modified: 2010-04-15
This is a bit weird.  I'm working with a windows application using C#.  I'm developing in Visual Studio.NET 2005.  I'm using the card.dll that comes with all PC's just so you know.  Each card that I'm using is a control.  I want to go through all the controls and if it's a card I remove it.  I have a loop:

foreach(Control ctr in this.Controls)
{
//do code
}

When I get a count of all the controls (this.Controls.Count) I get 72.  But in my foreach loop it's only going through 46 times.

I tried changing it to a for loop

intMyControlCount = this.Controls.Count;
for(int x = 0; x < intMyControlCount; x++)
{
//do code
}
but after it gets past 46 i get an index out of range error.  This doesn't make sense to me.  The count is returning 72.  

If I run that foreach loop like 4 times then it eventually goes through all of them  haha.  But not good obviously.  Can anyone help please?
0
Comment
Question by:rshafakian
  • 3
  • 2
5 Comments
 
LVL 22

Expert Comment

by:JimBrandley
ID: 20856260
I think your problem is that some controls contain controls. So you need to do this recursively.

public void ModifyChildControls( Control control)
{
   foreach(Control child in control.Controls)
   {
      ModifyChildControls( child );
   }
   // Now modify this control, then return
}

Jim
0
 

Author Comment

by:rshafakian
ID: 20856293
Yes I'm doing it recursively.  That's not it.  It's weird because if I output the count of the controls it's 72.  But when I'm looping through the controls it's only 46.  And If I run the exact same loop 4 or 5 times it eventually gets all the controls.  I'm removing the controls as I see them.  So I'm starting with 72,  when I go through it the first time it removes 26 controls, leaving me with 46.  Then like 13 more the second time around.  And then 7 more.  Etc...
0
 
LVL 22

Accepted Solution

by:
JimBrandley earned 2000 total points
ID: 20856306
Are you removing from the bottom up or the top down? That can make a difference. Notice in mine, the recursion step is befor the action.

Jim
0
 

Author Comment

by:rshafakian
ID: 20856313
Ohhhhh.  You're right.  I'm doing a for loop right now starting from 72 an going down to 0 and it works.  Thank you so much for your help!
0
 
LVL 22

Expert Comment

by:JimBrandley
ID: 20856392
My pleasure. Good luck.

Jim
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

608 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question