Wednesday, April 20, 2011

Removing controls in a loop

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?

From stackoverflow
  • 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.

    Marc Gravell : I'd love to know why the downvote...
    J W : Seems like a good answer to me. I upvoted it.
    Mitch Wheat : It wasn't me, but it's the correct answer, so +1!

0 comments:

Post a Comment