I'm currently trying to remove all the controls in a picturebox. I'm doing so like this:
Then I call addGrid() which looks like this:
But for whatever reason, when I try to remove all the controls I get an index out of range exception for item 512 every time. Is there something that I'm missing that would cause this to happen? Because I set a breakpoint on the first line of the for loop and it says my pbGrid.Controls.Count is 1024.
vb.net Code:
For i As Integer = 0 To pbGrid.Controls.Count - 1 pbGrid.Controls.RemoveAt(i) Next
Then I call addGrid() which looks like this:
vb.net Code:
Private Sub addGrid() Dim wid As Integer = 10 Dim hig As Integer = 10 For x As Integer = 0 To 31 For y As Integer = 0 To 31 Dim pb As New PictureBox With pb .BackColor = transparentColor .BorderStyle = BorderStyle.FixedSingle .Location = New Point(x * wid, y * hig) .Size = New Size(wid, hig) End With pbGrid.Controls.Add(pb) grid(x, y) = pb Next Next End Sub
But for whatever reason, when I try to remove all the controls I get an index out of range exception for item 512 every time. Is there something that I'm missing that would cause this to happen? Because I set a breakpoint on the first line of the for loop and it says my pbGrid.Controls.Count is 1024.