Rolling Dice Game
I am trying to make images change randomly in a picture box
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Emiliano MussoRank #619 · 3.9K reputationPosted May 6, 2015, 1:19 AM EDT
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 Kiafuka113 reputationPosted May 7, 2015, 11:06 AM EDT