Hey there,
My program works by having a blank form, then adding picturebox controls to the form. I need to reference these pictureboxes later on, and so I need to add each one to part of an array.

But when I am adding my picturebox (The Object) to MyGrid (My Grid), the program crashes saying i have exceeded the boundries of the array.

This is how it looks so far:

    Public Class Form1
    Dim Rows As Integer = 0
    Dim Collumns As Integer = 0
    Dim MyGrid(Rows, Collumns) As PictureBox

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            For Me.Rows = 0 To My.Settings.Rows
            For Me.Collumns = 0 To My.Settings.Collumns

                Dim Seatpicture As New PictureBox
                Seatpicture.Location = New Point(10 + (Collumns * 45), 15 + (Rows * 80))
                Seatpicture.ImageLocation = (My.Settings.Driveletter + ":\Program\Seats\White.gif")
                Seatpicture.SizeMode = PictureBoxSizeMode.StretchImage
                Seatpicture.Size = New Size(40, 78)
                Seatpicture.Load()

                AddHandler Seatpicture.MouseEnter, AddressOf Mouse_Enter
                AddHandler Seatpicture.MouseLeave, AddressOf Mouse_Leave
                AddHandler Seatpicture.MouseClick, AddressOf Mouse_Click

                Me.Controls.Add(Seatpicture)
                MyGrid(Rows, Collumns) = Seatpicture
                'This is where the error shows.
                'It works when rows and collumns are (0,0) but when it becomes
                '(0,1), then it shows the error

            Next
        Next
    End Sub

Can I get any help?
Thanks c:

Dani AI

Generated

Short answer: the array was created with the bounds that existed at declaration time, so your grid stayed 1×1 and indexing to (0,1) threw an error. already noticed you need to "redefine" the grid — the proper fixes are: allocate the array after you read the desired row/column counts, or use a resizable container (List/Dictionary) instead of a fixed 2D array.

Example — declare with the final sizes (VB arrays use upper bounds, so subtract 1):

Dim rowCount As Integer = Math.Max(1, My.Settings.Rows)
Dim colCount As Integer = Math.Max(1, My.Settings.Columns)
Dim seatGrid(rowCount - 1, colCount - 1) As PictureBox

For r As Integer = 0 To rowCount - 1
    For c As Integer = 0 To colCount - 1
        Dim pb As New PictureBox()
        ' initialize pb, add handlers, Controls.Add(pb)...
        seatGrid(r, c) = pb
    Next
Next

Alternatives and pitfalls

  • ReDim: you can ReDim an array, but ReDim Preserve only preserves the last dimension. That makes resizing a 2D grid with preservation awkward.
  • Dictionary/List: use a Dictionary(Of Point, PictureBox) or a single List(Of PictureBox) and compute index = r*cols + c. These avoid resizing issues and simplify lookups when rows/columns change. Example:
Dim seatMap As New Dictionary(Of Point, PictureBox)
seatMap.Add(New Point(c, r), pb)

Practical tips

  • Don’t reuse class fields like Rows/Columns as loop counters; use r/c or rowIndex/colIndex to avoid confusion.
  • Decide whether My.Settings stores a count or a zero-based max index; pick loop bounds accordingly (0 To count-1 for counts).
  • Consider a TableLayoutPanel or a custom Seat control for layout instead of calculating positions manually — it makes resizing and alignment easier.

This addresses the out-of-range exception and gives options depending on whether you need a fixed grid or a flexible structure.

I just found out, i needed to redefine the Grid to accomadate values Past (0,0)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.