Reputation: 95
Hey guys first of all thanks for your time
I am programming a board game and at one point I need 6 dices.
For 1 dice I did the following code
Dim Dobbel as integer
Dim RandomNumber as new Random
Dobbel = RandomNumber.Next(1, 6)
Select Case Dobbel
Case 1
Steen1.Image = Game.My.Resources.Een
Case 2
Steen1.Image = Game.My.Resources.Twee
Case 3
Steen1.Image = Game.My.Resources.Drie
Case 4
Steen1.Image = Game.My.Resources.Vier
Case 5
Steen1.Image = Game.My.Resources.Vijf
Case 6
Steen1.Image = Game.My.Resources.Zes
End Select
this way it works with 1 dice (the picturebox gets the right picture)
Now I would like 5 more dices to do the above. I've been trying this with a 'for each' statement but I couldn't get it to work. Help me plz
P.S. this is my first post ever sorry about things I did wrong
Upvotes: 1
Views: 865
Reputation: 81610
Assuming "couldn't get it to work" means you didn't know how to loop the six PictureBox controls, try putting them into an array:
For Each pb As PictureBox In New PictureBox() {steen1, steen2, steen3, steen4, steen5, steen6}
Select Case RandomNumber.Next(1, 7)
Case 1 : pb.Image = Game.My.Resources.Een
Case 2 : pb.Image = Game.My.Resources.Twee
Case 3 : pb.Image = Game.My.Resources.Drie
Case 4 : pb.Image = Game.My.Resources.Vier
Case 5 : pb.Image = Game.My.Resources.Vijf
Case 6 : pb.Image = Game.My.Resources.Zes
End Select
Next
This assumes you have six PictureBoxes named steen#, etc.
Also note that I changed your Random range to 1 - 7. The max is one less, so in your code, you were never getting a number 6 for an image.
Upvotes: 1
Reputation: 761
Dim Dobbel(6) as integer
Dim RandomNumber as new Random
for (int = 0; i < 5; i++)
{
Dobbel(i) = RandomNumber.Next(1, 6)
}
For Each i As Integer In Dobbel
{
Select Case Dobbel(i)
Case 1
Steen(i).Image = Game.My.Resources.Een
Case 2
Steen(i).Image = Game.My.Resources.Twee
Case 3
Steen(i).Image = Game.My.Resources.Drie
Case 4
Steen(i).Image = Game.My.Resources.Vier
Case 5
Steen(i).Image = Game.My.Resources.Vijf
Case 6
Steen(i).Image = Game.My.Resources.Zes
End Select
}
and of course Steen must be declared as array.
Upvotes: 0