This has happened to me many times. Although I have found a way around it, it feels unprofessional and I would like to know why this happens.
Suppose I have a FlowLayoutPanel
with several 10s of buttons therein.
I intend to loop through all controls and check if a certain condition is met. If it is, then I wish to remove this control. So I'd do something like this:
foreach (Control c in flp_p_products.Controls)
{
if(condition(c))
flp_p_products.Controls.Remove(c);
}
However, on more than one occasion, in different projects, it is not rare that I find that one control manages to slip out of the loop - and I do not know why.
It is only when I add
while(flp_p_controls.Count > 0)
at the beginning of my foreach...
loop that all of the children are looped through.
Is there something I'm not realizing?