I think you are going about this the wrong way. This is how I would do it. I am assuming you are trying to get random results from 5 dice, and setting the image for 5 pb's to correspond to each number rolled. I aslo assume your imageList contains six images which correspond to the six sides of a dice. First off I would store my PictureBoxes in a List<PictureBox> . That way I could access them from a loop. I would also have a List<int> to store my dice results, so i would declare them as form level variables, along with an int to store the total rolled and an instance of the Random class to generate random results :-
Random r = new Random();
List<int> diceResults = new List<int>();
List<PictureBox> dice = new List<PictureBox>();
int diceTotal;
Then I would add my PictureBoxes to my dice list in the form constructor(could also be done in FormLoad event) like this;
public Form1()
{
InitializeComponent();
dice.Add(pictureBox1);
dice.Add(pictureBox2);
dice.Add(pictureBox3);
dice.Add(pictureBox4);
dice.Add(pictureBox5);
}
then in the click event of the roll button, you just construct a loop to generate random numbers and add as you go like this:-
private void button1_Click(object sender, EventArgs e)
{
diceTotal = 0;
diceResults.Clear();
for (int i = 0; i <= 4; i++)
{
diceResults.Add(r.Next(1,6));
int imageNumber = diceResults[i] - 1;
dice[i].Image = this.imageList1.Images[imageNumber];
diceTotal += diceResults[i];
}
lblDiceTotal.Text = diceTotal.ToString();
}
notice i used 1 as the minimum for my random generator so I don't get 0 as a result, this means I must subtract 1 when want to generate the image index.
Hope this helps