Problem with clearing controls in application

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?
rshafakianAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JimBrandleyCommented:
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
rshafakianAuthor Commented:
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
JimBrandleyCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rshafakianAuthor Commented:
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
JimBrandleyCommented:
My pleasure. Good luck.

Jim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.