Ive just been using vb2005 for a couple of days and i have discovered that it desnt support control arrays the way vb6 and previous did. How do i write code that controls 150 picture boxes! heres an example,
Private Sub Timer1_Timer()
Dim Value
Dim a As Integer
a = 0
Do While a <= 150
Randomize
Value = Int((4 * Rnd) + 1) ' Generate random value between 1 and 4.
If Value = 1 Then picBox(a).Picture = picBlue.Picture
If Value = 2 Then picBox(a).Picture = picRed.Picture
If Value = 3 Then picBox(a).Picture = picGreen.Picture
If Value = 4 Then picBox(a).Picture = picWhite.Picture
a = a + 1
Loop
End Sub
Can any1 tell me how this code will look in visual basic 2005
Why did they have to change control arrays!
Thanks

Control Arrays
kuria
They chagned it because collections are far better.
To get your answer, just take your exact thread title and put it in the search bar of this forum. Your answer is on the first page of results.
amb_lew
i managed to do it: took me 2 days but I managed. i am going to use buttons instead of picture boxes so that i can use tha TabIndex of the buttons to act as "indexes", picture boxes dont have tabindexes.
dim a as integer 'tabindex for the buttons btwn 0 and 150
dim value as integer
Private Sub Timesr1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Randomize()
value = Int((4 * Rnd()) + 1) 'generates a rando number between 1 and 4
Dim Box As Button
For Each Box In Me.Controls 'find all the buttons in the form
If TypeOf Box Is Button Then 'this part might not be neccesary as Box is defined as button
If Box.TabIndex = a Then 'if the tabindex of the box is between 0 and 150 then change its background image
If Value = 1 Then Box.BackgroundImage = picBlue.Image
If Value = 2 Then Box.BackgroundImage = picRed.Image
If Value = 3 Then Box.BackgroundImage = picGreen.Image
If Value = 4 Then Box.BackgroundImage = picWhite.Image
End If
End If
Next
a += 1
If a = 151 Then Timer1.Enabled = False
Exit Sub
End Sub
its easier to do it this way
Thanks
Wai Hon
Hi-
This is definitely a FAQ.
Here are a few details to get you started:
- You can use the Collections.Generic.List(Of T) object as a nice container for your pictureboxes. Simply replace T with the type you want to use, like PictureBox. E.g.:
Dim picBox As New Collections.Generic.List(Of PictureBox)- You can work with generic List objects by index similarly to the VB6 array, and you even get nice Intellisense now, e.g.
picBox(2).Image
- Don't forget that arrays and collections in VB 2005 are zero-based, so they start indexing at 0.
- Here is a sample using the generic list. Timer1_Timer is very similar to your existing code. I included Timer1_Timer_Rewritten to show you how you can loop through the list with less code:
'defined by WinForms designer Dim picBlue As New PictureBox Dim picRed As New PictureBox Dim picGreen As New PictureBox Dim picWhite As New PictureBox
'generic List container used to replace the control array Dim picBox As New Collections.Generic.List(Of PictureBox)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Initialize picturebox collection and containing objectspicBox =
New Collections.Generic.List(Of PictureBox) For a As Integer = 0 To 150 New PictureBox) Next 'assign random images to picboxes Me.Timer1_Timer() Me.Timer1_Timer_Rewritten() End Sub Private Sub Timer1_Timer() Dim Value As Integer Dim a As Integera = 0
Do While a <= 150 ' Generate random value between 1 and 4. If Value = 1 Then picBox(a).Image = picBlue.Image If Value = 2 Then picBox(a).Image = picRed.Image If Value = 3 Then picBox(a).Image = picGreen.Image If Value = 4 Then picBox(a).Image = picWhite.Imagea = a + 1
Loop End Sub Private Sub Timer1_Timer_Rewritten() For Each picBoxObj As PictureBox In picBox Dim Value As Integer = Int((4 * Rnd()) + 1) ' Generate random value between 1 and 4. If Value = 1 Then picBoxObj.Image = picBlue.Image If Value = 2 Then picBoxObj.Image = picRed.Image If Value = 3 Then picBoxObj.Image = picGreen.Image If Value = 4 Then picBoxObj.Image = picWhite.Image Next End Sub