title: Removing Dynamically created controls in Panel
body: <p>I have spent hours trying to understand what is the problem in my coding but without any much result. Hence thought of posting here for an explanation</p>
  
  <p>In my UI design, I have first row of <code>&lt;&lt;checkbox, textbox, textbox&gt;&gt;</code> in this order. And depending on the number of rows of result returned, I will dynamically create the same number of rows of checkbox and textboxes.</p>
  
  <p>However when I try to remove the redundant rows, leaving only row 1 intact, I noticed that only some controls are being removed.</p>
  
  <p>For e.g</p>
  
  <p>Before deleted:</p>
  
  <pre><code>checkbox0 textbox0 tb0
  
  checkbox1 textbox1 tb1
  
  checkbox2 textbox2 tb2
  </code></pre>
  
  <p>After executing</p>
  
  <pre><code>checkbox0 textbox0 tb0
  
  --------- -------- tb1
  
  checkbox2 textbox2 ---
  </code></pre>
  
  <p>My code for removing is as below:</p>
  
  <pre><code>foreach (Control c in panel1.Controls)
  {                
    lastChar = c.Name[c.Name.Length - 1];
    lastValue = (int)Char.GetNumericValue(lastChar);
    MessageBox.Show("second " + c.Name);
  
    if (lastValue &gt; 0 &amp;&amp; lastValue &lt; count)
    {
      panel1.Controls.Remove(c);
      c.Dispose();
    }
   }
  </code></pre>
  
  <p>Rightfully foreach will run through all the controls within the panel1. But i noticed that once the Remove statement is in, the whole operation went haywire.</p>