Quantcast
Viewing all articles
Browse latest Browse all 27078

[RESOLVED] Odd index out of range exception

I'm currently trying to remove all the controls in a picturebox. I'm doing so like this:
vb.net Code:
  1. For i As Integer = 0 To pbGrid.Controls.Count - 1
  2.             pbGrid.Controls.RemoveAt(i)
  3.         Next

Then I call addGrid() which looks like this:
vb.net Code:
  1. Private Sub addGrid()
  2.         Dim wid As Integer = 10
  3.         Dim hig As Integer = 10
  4.  
  5.         For x As Integer = 0 To 31
  6.             For y As Integer = 0 To 31
  7.                 Dim pb As New PictureBox
  8.                 With pb
  9.                     .BackColor = transparentColor
  10.                     .BorderStyle = BorderStyle.FixedSingle
  11.                     .Location = New Point(x * wid, y * hig)
  12.                     .Size = New Size(wid, hig)
  13.                 End With
  14.                 pbGrid.Controls.Add(pb)
  15.  
  16.                 grid(x, y) = pb
  17.             Next
  18.         Next
  19.     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.

Viewing all articles
Browse latest Browse all 27078

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>