Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing controls in a loop

Tags:

c#

.net

winforms

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?

like image 860
Hao Wooi Lim Avatar asked Apr 10 '09 07:04

Hao Wooi Lim


1 Answers

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.

like image 56
Marc Gravell Avatar answered Nov 06 '22 17:11

Marc Gravell