You, in general, cannot remove from the collection, iterations of the enumerable from it. Instead of using foreach, a typical approach is to use a for loop that works in the opposite direction:
private void ClearSearchResults() { for(int i=panel1.Controls.Count-1;i>=0;--i) { panel1.Controls.RemoveAt(i);
However, in this case, just use clear:
panel1.Controls.Clear();
source share