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.