Controls collection

  • Thread starter Thread starter Charlie Brown
  • Start date Start date
C

Charlie Brown

I am adding controls to the Forms controls collection at run-time.
Then when I loop through the controls and list them by name, I am
missing half my controls. Is there a workaround?

Debugger output while adding

shiftmrk0 Added
shiftmrk1 Added
shiftmrk2 Added
shiftmrk3 Added
shiftmrk4 Added

Debugger output while looping through the forms controls() collection

shiftmrk0
shiftmrk2
shiftmrk4

All controls are children of the Form, no panels or tab controls.
 
For i as integer = 0 to 4
Dim mrk as new Label
lbl.name = "shiftmrk" & i
me.controls.add(mrk)
Debug.writeline(mrk.name & " Added")
Next

For each ctrl as Control In Me.Controls()
If Not ctrl is Nothing
If ctrl.gettype is gettype(Label)
ctrl.dispose
debug.writeline(mrk.name & " Removed")
End If
End If
Next

Typed this by hand quick, but my code is the same.
 
I have found a temp solution to the problem, although there must be a
better way. I added enlosed the For...Next in another loop and cycle
through it once more.

For i as integer = 0 to 1
For each ctrl as Control In Me.Controls()
If Not ctrl is Nothing
If ctrl.gettype is gettype(Label)
ctrl.dispose
debug.writeline(mrk.name & " Removed")
End If
End If
Next
Next

This removes all controls, but it shouldn't have to be done this way.
 
Back
Top