Click to See Complete Forum and Search --> : picturebox array


Steve Putt
August 6th, 2002, 08:34 AM
I'm trying to fill my form with a 2-d array of pictureboxes. I need to refer to them in code like this - arrPbox(row, column). I'm stumped without an index property. How can insert them with a 2-d index-like reference in design, or if not then, create them at runtime? (Yes, I'm a rookie!)
I tried runtime with this, but the picture boxes don't even show up...

Dim arrPBox(9, 14) As PictureBox

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim bytRow, bytCol As Byte
For bytRow = 0 To 9
For bytCol = 0 To 14
arrPBox(bytRow, bytCol) = New System.Windows.Forms.PictureBox()
With arrPBox(bytRow, bytCol)
.BackColor = System.Drawing.Color.DarkGreen
.SetBounds((bytRow * 40), (bytCol * 40), 40, 40)
.TabStop = False
.Visible = True
.BringToFront()
End With
Next
Next
End Sub

Some of the With stuff is different things I did to try to see them.
Thanks a lot .... Steve

DSJ
August 6th, 2002, 09:04 AM
I think you'll also need to set top, left, bottom and right properties to position them on your form.

Steve Putt
August 6th, 2002, 09:22 AM
I tried top, left, height, and width - didn't work, that's when I found the SetBounds property, supposed to be all of the above. Maybe I didn't code the positions and sizes right. Thanks.

DSJ
August 6th, 2002, 09:58 AM
You have to also add them to the forms controls collection:

Private PictureBoxes(10) As PictureBox
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
Me.SuspendLayout()
For i = 1 To 10
PictureBoxes(i) = New System.Windows.Forms.PictureBox()
PictureBoxes(i).Location = New System.Drawing.Point(16 * i, 16 * i)
PictureBoxes(i).Name = "PictureBox" & i
PictureBoxes(i).BorderStyle = BorderStyle.Fixed3D
PictureBoxes(i).Size = New System.Drawing.Size(32, 24)
PictureBoxes(i).TabIndex = i - 1
PictureBoxes(i).TabStop = False
PictureBoxes(i).BackColor = System.Drawing.Color.Red
PictureBoxes(i).Visible = True
'************** NEED NEXT LINE !!!**************'
Me.Controls.AddRange(New System.Windows.Forms.Control() {PictureBoxes(i)})
Next i
Me.ResumeLayout(False)
End Sub

DSJ
August 6th, 2002, 09:59 AM
You have to also add them to the forms controls collection:

Private PictureBoxes(10) As PictureBox
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
Me.SuspendLayout()
For i = 1 To 10
PictureBoxes(i) = New System.Windows.Forms.PictureBox()
PictureBoxes(i).Location = New System.Drawing.Point(16 * i, 16 * i)
PictureBoxes(i).Name = "PictureBox" & i
PictureBoxes(i).BorderStyle = BorderStyle.Fixed3D
PictureBoxes(i).Size = New System.Drawing.Size(32, 24)
PictureBoxes(i).BackColor = System.Drawing.Color.Red
PictureBoxes(i).Visible = True
'************** NEED NEXT LINE !!!**************'
Me.Controls.AddRange(New System.Windows.Forms.Control() {PictureBoxes(i)})
Next i
Me.ResumeLayout(False)
End Sub