0

New to programming. I'm working on a Windows Form Application for the Petals Around the Rose game for school, and although our instructor has shown us how to change individual pictureboxes upon clicking then, I want to make a button that can change all of them at once/'reroll' the dice.

Some code:

    private void frmDisplay_Load(object sender, EventArgs e)
    {
        // Create list object to hold dice images
        diceArray = new List<MyPictureBox>();

        for (int i = 0; i < 5; i++)
        {
            MyPictureBox picBox = new MyPictureBox(i + 1);
            picBox.Image = RandomDice.GetRandomDice();
            picBox.Click += new System.EventHandler(pictureBox_Click);
            this.Controls.Add(picBox);
            this.Controls.Add(picBox.TheLabel);
        }           
    }

The above code creates 5 pictureboxes when the form loads, and pulls from my RandomDice class, here:

class RandomDice
{
    private static Random randomGen = new Random();
    private static Image[] imgList = new Image[6];

    public static Image GetRandomDice()
    {
        int i = 0;
        ResourceSet rsrcSet = Properties.Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentCulture, true, true);
        foreach (DictionaryEntry entry in rsrcSet)
        {
            imgList[i] = (Image) entry.Value;
            i++;
        }
        i = randomGen.Next(0, imgList.Length);
        return imgList[i];
    }
}

I have an event set up for a button click, but I'm not sure how to make it so the button changes all my dice images at once.

Any advice?

Thank you!

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Kim
  • 1

1 Answers1

0

You have to iterate through the array. And then change the image inside your for loop.

    /// <summary>Update the image list with random dice images.</summary>
    public static void UpdateDices()
    {
        // Iterate through the image array.
        for (var i = 0; i < imgList.Length; i++)
        {
            // Update the dice image.
            imgList[i] = GetRandomDice();
        }
    }
  • I tried your suggestion but no dice (heh heh). However, this gives me ideas, thank you! – Kim Jul 09 '18 at 21:19