Domanda

I create controls dynamically (checkBoxes, listBox, trackBar...) on a Form, and i put menuStrip using the editor. I'd like to remove all the controls, and i'd like to keep the menu. When i use this line:

this.Controls.Clear();

it removes the menu as well as the controls, however as i understood, menu items are not controls, they are on the form directly(i can see them if i write "this" and press a dot). i tried to cycle over the controls and removed only if the type was one of the controls, but some control stayed while some are removed. I cycled using controls.Count. I tried to put the whole cycle to another while() and exit if Controls.Count > 1 like this:

while( this.Controls.Count > 1 )
{
    for (int i = 0; i < this.Controls.Count; i++ )
    {
        if ((this.Controls[i].GetType() != typeof(MenuStrip)) ) 
        {
            this.Controls.RemoveAt(i);
        }
    }
}

It removes the controls and leave the menu alone, but the items that are disappears doesn't disapper in the same time, but some time later, i guess it's becouse the while runs more than one time.
My questions:
1. Why can't it remove all the control at once, while i iterate over the whole thing using controls.count as the upper bound.
2. What's the point of menuStrip as control while toolStripMenuItmes are not control.

È stato utile?

Soluzione

This is the classic mistake of modifying the collection that you are iterating. Most collection classes generate an exception when you do that, unfortunately ControlCollection doesn't. It just misbehaves, you'll skip the next control after the one you remove. There's another nasty bug, the Controls.RemoveAt() method doesn't dispose the control you remove, it leaks forever.

Avoid the iteration bug by iterating backwards. And properly dispose, like this:

for (int i = this.Controls.Count-1; i >= 0; i--) {
    if (this.Controls[i].GetType() != typeof(MenuStrip)) {
        this.Controls[i].Dispose();
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top