Question Control Arrays

robtyketto

Member
Joined
Jul 23, 2009
Messages
23
Programming Experience
Beginner
Greetings,

I shied away from using structures (see previous post) and thought about using collections or control away to track related picture boxes.

I went with control arrays (see code below).

After some reasearch I believe control arrays are difficult to use and the code below will never work.

I have another version of the code that loops around using for each control and then checking the control type and then where name like 'xxx*"

How can I collectively store all the picturebox controls in one group and reference them by an index value?

VB.NET:
Dim dice() As PictureBox = {picDice1, picDice2, picDice3, picDice4, picDice5, picDice6}

   'Declare variable to hold index of picture in imagelist, rand for a random num
        'and ctrlvar as control to enable looping through all controls
        Dim intPic As Integer
        Dim rand As New Random
        Dim index As Integer = 0

        'Assign random dice head/value to the six dice
        For Each die As PictureBox In dice

            ' Pick a random number between 0 and the number of images in our imagelist control
            intPic = rand.Next(0, ImageList1.Images.Count)

            'die(0).Image = ImageList1.Images(intPic)
            dice(0).Image = ImageList1.Images(intPic)
            index += 1

        Next

Thanks
Rob
 
Really bud, your making this much harder then needed to display a few different variations of dice pictures. You using what, a six sided die for example, add all six of the photos into an image list or right into the project resources, then when they roll the dice, if its a one, display the die picture associated for one. I dont see where you need arrays, datatables, collections any of that... If Im missing the point, please explain your program better and I will try to help
 
It's for a game of Yahtzee and as they roll FIVE dice in one throw I wanted to assign values in loops.

Rather than picbox1 = pic1, picbox2 = pic3, picbox4 = pic6 etc..,

This code In this post works to assign values but interested in how I could use an index value to reference the picture box.

I'm a newbie to vb.net and really relatively new to programming in general so Im interested in all kinds of solutions and what can/can't be utilised.

VB.NET:
 'Declare variable to hold index of picture in imagelist, rand for a random num
        'and ctrlvar as control to enable looping through all controls
        Dim intPic As Integer
        Dim rand As New Random
        Dim CtrlVar As Control

        'Assign random dice head/value to the six dice
        For Each CtrlVar In Controls

            If TypeOf CtrlVar Is PictureBox And CtrlVar.Name Like "picDice*" Then

                ' Pick a random number between 0 and the number of images in our imagelist control
                intPic = rand.Next(0, ImageList1.Images.Count)

                With CType(CtrlVar, PictureBox)
                    .Image = ImageList1.Images(intPic)
                End With

            End If

        Next
 
You can create your own List(of PictureBox).

VB.NET:
dim diceList as new List(of PictureBox)
diceList.add(picDice1)
diceList.add(picDice2)
...

You could then the index to reference the picturebox
 
You can create your own List(of PictureBox).

VB.NET:
dim diceList as new List(of PictureBox)
diceList.add(picDice1)
diceList.add(picDice2)
...

You could then the index to reference the picturebox

Thanks I will read up more about lists and do some experiments tomorrow :)
 
In VB.Net control arrays is replaced with control collections, each form/control has a Controls property that expose this and contains the controls parented by this control. The Controls collection can be indexed by Integer position or String name, f.ex Me.Controls(0) or Me.Controls("PictureBox1"). If you place perhaps six pictureboxes in a Panel or GroupBox and no other controls you can iterate only the pictureboxes with For Each in Me.PicsPanel.Controls or For-Next the indexes 0 to 5 with same collection. The items in the control collection is type Control, which is a general type and thus can contain any type of control, if you need to access specific type member from an inherited control like the PictureBox.Image property you can cast the control object to this type using either CType or DirectCast. There is also the OfType(TResult) filtering extension method that can be used, but get aquainted with how .Net arranges controls first and how to handle them.
 
In VB.Net control arrays is replaced with control collections, each form/control has a Controls property that expose this and contains the controls parented by this control. The Controls collection can be indexed by Integer position or String name, f.ex Me.Controls(0) or Me.Controls("PictureBox1"). If you place perhaps six pictureboxes in a Panel or GroupBox and no other controls you can iterate only the pictureboxes with For Each in Me.PicsPanel.Controls or For-Next the indexes 0 to 5 with same collection. The items in the control collection is type Control, which is a general type and thus can contain any type of control, if you need to access specific type member from an inherited control like the PictureBox.Image property you can cast the control object to this type using either CType or DirectCast. There is also the OfType(TResult) filtering extension method that can be used, but get aquainted with how .Net arranges controls first and how to handle them.

Thanks for all the advice.

I used a panel and now sucessfully use a for each/next to apply setting to my picture boxes.

Happy :p
 
Back
Top