Help deleting some controls in control array

ministreak

Member
Joined
Apr 14, 2006
Messages
11
Programming Experience
5-10
Hey guys, I need a little help here...

I can create a control array of checkboxes at runtime. Problem is I also need to delete and dispose of then at runtime.

Lets say I have created 25 checkboxes at runtime with their indexes starting at 0... Using the code below it wil delete every other checkbox and leave the rest...

These checkboxes are created in a panel,,,

VB.NET:
Dim ChkBox(25) As CheckBox [COLOR=seagreen]'''array for the 25 checkboxes... this is variable at runtime[/COLOR]
Dim intChangingCheckBoxLeftValue As Integer
Dim intNumberOfCheckBoxesDrawnThusFar As Integer = 0
Dim NumberOfCheckBoxesToDraw As Integer = 25
Dim NumberOfCheckBoxesAllowedPerRow As Integer = 6
Dim dblCheckBoxLocationFromTop As Double = 84
Dim intHorizontalCheckBoxSpace As Integer = 49
Dim intVerticalCheckBoxSpace As Integer = 18
Dim intChangingVerticalCheckBoxSpaceValue As Integer
Dim intFirstCheckBoxLocationFromLeft As Integer = 5
 
[COLOR=seagreen]'''Create some check boxes[/COLOR]
     For x As Integer = 1 To NumberOfCheckBoxesToDraw
            ChkBox(x) = New CheckBox
            ChkBox(x).TabIndex = x
            ChkBox(x).Enabled = True
            ChkBox(x).Name = "chkSomeBox"
            ChkBox(x).Size = New System.Drawing.Size(44, 16)
            ChkBox(x).Text = (x)
 
            [COLOR=seagreen]'''This section will decide where to place the checkBoxes from the left of each other[/COLOR]
            If intNumberOfCheckBoxesDrawnThusFar = 0 Then
                intChangingCheckBoxLeftValue = intFirstCheckBoxLocationFromLeft
            Else
                intChangingCheckBoxLeftValue = ChkBox(x - 1).Left + ChkBox(x - 1).Width
            End If
 
            [COLOR=seagreen]'''This section decides when to make a new row[/COLOR]
            If intNumberOfCheckBoxesDrawnThusFar = (NumberOfCheckBoxesAllowedPerRow) Then
                intNumberOfCheckBoxesDrawnThusFar = 0 [COLOR=seagreen]'Reset to zero to count the new row[/COLOR]
                intChangingCheckBoxLeftValue = intFirstCheckBoxLocationFromLeft 'Start the new row of boxes this far from the left
                intChangingVerticalCheckBoxSpaceValue += intVerticalCheckBoxSpace 'Space the next row down this much
            End If
 
           [COLOR=seagreen]'''This draws the checkBoxes and adds them to the panel[/COLOR]
            Me.pnlCheckBoxHolder.Controls.Add(ChkBox(x))
 
             ChkBox(x).Location = New System.Drawing.Point(intChangingCheckBoxLeftValue, (dblCheckBoxLocationFromTop + intChangingVerticalCheckBoxSpaceValue))   '(x * 10, x * 10)
 
[COLOR=seagreen]           'Count the number of boxes that have been drawn. (this resets after each row)[/COLOR]
            intNumberOfCheckBoxesDrawnThusFar += 1            
     Next
 
 
[COLOR=seagreen]'''///////////////////////////////////////////////////////////////////////////////////////[/COLOR]
 
 
 
[COLOR=seagreen]'''Try and delete the checkboxes[/COLOR]
Dim ctrl_ As Control
        For Each ctrl_ In Me.pnlSomePanel.Controls
            If TypeOf (ctrl_) Is CheckBox Then
                Me.pnlSomePanel.Controls.Remove(ctrl_)
                MsgBox("Checkbox : " & Chr(34) & ctrl_.Text & Chr(34) & " has been removed.")
             End If
        Next

This method of deleting the boxes doesn't work but it does delete every other one for some reason. Using Dispose doesn't work either.

Why does it delete every-other checkbox?

Thanks for looking,
Joe
 
"When a Control is removed from the control collection, all subsequent controls are moved up one position in the collection."
VB.NET:
Dim ctrl_ As Control 
For i As Short = Me.pnlSomePanel.Controls.Count -1 to 0 Step -1
  ctrl_ = Me.pnlSomePanel.Controls(i)
  If TypeOf (ctrl_) Is CheckBox Then
    MsgBox("Checkbox : """ & ctrl_.Text & """ has been removed.")
    Me.pnlSomePanel.Controls.Remove(ctrl_)
  End If 
Next
 
Back
Top