Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 198489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:56:31+00:00 2026-05-11T16:56:31+00:00

Yesterday I wrote a piece of code to remove all the controls in a

  • 0

Yesterday I wrote a piece of code to remove all the controls in a form that fulfills certain criteria. Writing it naively, this is what I come up with.

for (int i = 0; i < this.Controls.Count; ++i)
{
    if (this.Controls[i].Name.Length == 2)
    {
        this.Controls.Remove(this.Controls[i);
    }
}

But it so happens that the code is wrong. I then change it to:

foreach (Control ctr in this.pbBoardImage.Controls)
{
    if (ctr.Length == 2)
    {
        this.Controls.Remove(ctr);
    }
}

But it still wasn’t correct.
I know that the correct way would be:

for (int i = this.Controls.Count - 1; i >= 0; i--)
{
    if (this.Controls[i].Name.Length == 2)
    {
        this.Controls.Remove(this.Controls[i]);
    }
}

However it still doesn’t feel elegant. I couldn’t use List.RemoveAll, since this.Controls wasn’t a List. So can I ask for a more elegant way, preferably without using a loop?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:56:31+00:00Added an answer on May 11, 2026 at 4:56 pm

    Not sure why you didn’t like this answer… I’ve highlighted the important RemoveAt; however, as an alternative in .NET 3.5/C# 3.0: LINQ:

            var qry = from Control control in Controls
                      where control.Name.Length == 2
                      select control;
    
            foreach(var control in qry.ToList()) {
                Controls.Remove(control);
            }
    

    (original)

    You can’t Remove within foreach – it breaks the iterator. A common approach here is to iterate backwards:

    for (int i = this.Controls.Count - 1; i >= 0; i--) {
        if (this.Controls[i].Name.Length == 2) {
            this.Controls.RemoveAt(i); // <=========== *** RemoveAt
        }
    }
    

    This avoids the “off by one” issues, etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Yesterday Night, i wrote this function to fetch all the tags from string containing
Yesterday I wrote some code to pull a collection of products. This code worked
I'm writing this question with reference to this one which I wrote yesterday. After
Yesterday I updated to firefox 13 and I notice that this property is not
Yesterday I wrote my first lines of code using the new dynamic type in
I wrote a program in Python yesterday that checks hundreds of URLs for validity
I asked about this yesterday , but I'm still having problems. I wrote a
Yesterday I discovered an odd bug in rather simple code that basically gets text
Yesterday I wrote code to automatically send emails upon receiving an IPN notification from
I asked this question yesterday, but I wrote it far more complicated than I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.