Rolling Dice Game
I am trying to make images change randomly in a picture box
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Emiliano MussoPosted May 6, 2015, 5:19 AM
private void button1_Click(object sender, EventArgs e)
{
// Defines a List of 6 paths, each for a single image
List
ImgPaths.Add(@"C:\tmp\face01.jpg");
ImgPaths.Add(@"C:\tmp\face02.jpg");
ImgPaths.Add(@"C:\tmp\face03.jpg");
ImgPaths.Add(@"C:\tmp\face04.jpg");
ImgPaths.Add(@"C:\tmp\face05.jpg");
ImgPaths.Add(@"C:\tmp\face06.jpg");
// Define a random number generator, and execute it
Random n = new Random();
int dice = n.Next(1, 7); //lower inclusive, upper exclusive
// Shows a MessageBox containing the random dice number, between 1 and 6
MessageBox.Show(dice.ToString());
// Load the image into the PictureBox, using the paths defined in ImgPaths
pictureBox1.Image = Image.FromFile(ImgPaths[dice-1]);
}
Here, we define a list of 6 physical paths for our images. Next, every time a random number is generated, we extract from our list the path corresponding to the extracted element, using it to change the image.
Hope this helps
Bruno KiafukaPosted May 7, 2015, 3:06 PM