I wanted to delete all of the TextBoxes that I put on a form that start with "tbx". The below code only deleted some of them.
For Each ctl As Control In Panel1.Controls
If ctl.Name.StartsWith("tbx") Then
ctl.Dispose()
End If
Next
So, I took it a step further in testing...
Now I'm really confused about the .controls collection. When I try to delete controls from Panel1 with the below code, it only deletes every other control instead of all controls.
For Each ctl As Control In Panel1.Controls
ctl.Dispose()
Next
It looks like it deletes item(0) which moves everything back one step but then advances the pointer to the next step.
For example, if the following controls exist on the form...
tbx1
tbx2
tbx3
tbx4
on the first pass it disposes of tbx1 but the next iteration though the for each loop now points to tbx3, thus bypasssing tbx2.
Any explanation on why this is doing this and how to get around it?