title: VB.NET Loop through controls in a panel skips controls
body: <p>Written a quick subroutine in a class to move controls from one <code>Panel</code> to another in VB.NET, which seemed simple enough:</p>
  
  <pre><code>Public Sub Move(ByRef OldPanel As System.Windows.Forms.Panel)
      Dim panelControl As System.Windows.Forms.Control
      For Each panelControl In OldPanel.Controls
          MessageBox.Show(panelControl.Name) 'Debugging
          OldPanel.Controls.Remove(panelControl) 'Fairly certain this line makes no difference
          NewPanel.Controls.Add(panelControl)
      Next
  End Sub
  </code></pre>
  
  <p>The problem is, it only moves about half the controls. The other panels aren't picked up by the loop at all and remain bound to <code>OldPanel</code>. I have verified that the controls are definitely part of the <code>OldPanel</code> (and not just visually floated above it).</p>
  
  <p>For example, if there are 6 controls on the panel, <code>MessageBox.Show(panelControl.Name)</code> only feeds back 3 of them, and only those 3 controls move. This is... baffling.</p>
  
  <p>I wrote a similar debugging loop inside the form class <code>_Load</code> event itself and this correctly picks up all 6 controls on the panel:</p>
  
  <pre><code>Dim panelControl As System.Windows.Forms.Control
  For Each panelControl In Me.Panel1.Controls
      MessageBox.Show(panelControl.name)
  Next
  </code></pre>
  
  <p>Any ideas?</p>