Written a quick subroutine in a class to move controls from one Panel
to another in VB.NET, which seemed simple enough:
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
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 OldPanel
. I have verified that the controls are definitely part of the OldPanel
(and not just visually floated above it).
For example, if there are 6 controls on the panel, MessageBox.Show(panelControl.Name)
only feeds back 3 of them, and only those 3 controls move. This is... baffling.
I wrote a similar debugging loop inside the form class _Load
event itself and this correctly picks up all 6 controls on the panel:
Dim panelControl As System.Windows.Forms.Control
For Each panelControl In Me.Panel1.Controls
MessageBox.Show(panelControl.name)
Next
Any ideas?