i don't know how use to a timer.
I'm supposed to do is After the Roll button is pressed, a timer will be enabled to generate random die rolls every 1/10 of a second...

public partial class Form1 : Form
    {
        Random m_rnd = new Random();
        List<int> diceResults = new List<int>();
        List<PictureBox> dice = new List<PictureBox>();
        int diceTotal;
        private int[] i_Array = new int[5] { 0, 1, 2, 3, 4 };
        public Form1()
        {
            InitializeComponent();
            
            pictureBox1.Image = imageList1.Images[i_Array[0]];
            pictureBox2.Image = imageList1.Images[i_Array[1]];
            pictureBox3.Image = imageList1.Images[i_Array[2]];
            pictureBox4.Image = imageList1.Images[i_Array[3]];
            pictureBox5.Image = imageList1.Images[i_Array[4]];
            dice.Add(pictureBox1);
            dice.Add(pictureBox2);
            dice.Add(pictureBox3);
            dice.Add(pictureBox4);
            dice.Add(pictureBox5);
        }

        private void btn_roll_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = imageList1.Images[m_rnd.Next(0, 6)];
            pictureBox2.Image = imageList1.Images[m_rnd.Next(0, 6)];
            pictureBox3.Image = imageList1.Images[m_rnd.Next(0, 6)];
            pictureBox4.Image = imageList1.Images[m_rnd.Next(0, 6)];
            pictureBox5.Image = imageList1.Images[m_rnd.Next(0, 6)];
            diceTotal = 0;
            diceResults.Clear();
            for (int i = 0; i <= 4; i++)
            {
                diceResults.Add(m_rnd.Next(1, 6));
                int imageNumber = diceResults[i] - 1;
                dice[i].Image = this.imageList1.Images[imageNumber];
                diceTotal += diceResults[i];
            }
            lbl_totalscore.Text = diceTotal.ToString();

        }

        private void timer1_Tick(object sender, EventArgs e)
        {

        }

        private void btn_playagain_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = imageList1.Images[i_Array[0]];
            pictureBox2.Image = imageList1.Images[i_Array[1]];
            pictureBox3.Image = imageList1.Images[i_Array[2]];
            pictureBox4.Image = imageList1.Images[i_Array[3]];
            pictureBox5.Image = imageList1.Images[i_Array[4]];
            lbl_totalscore.Text = "0";
            lbl_rollscore.Text = "0";
        }
    }

Recommended Answers

All 2 Replies

Everything is working just fine, there is one left to do and I'm having a hard time doing this:
When a label is clicked, the background color will be changed to red, indicating that that particular die will not be rolled. If the user clicks on a red die again, the color will return to green, indicating that that die will be rolled.

i was able to change the picturebox background once. I mean once i click the picturebox the background change into red, but when i clicked it again, it won't change the background back into green.

Random m_rnd = new Random();
        List<int> diceResults = new List<int>();
        List<PictureBox> die = new List<PictureBox>();
        PictureBox[] dice = null;
        int i_diceTotal = 0;
        int[] i_Array = new int[5];
        int rolls = 0;
        int i_rolling = 0;

        public Form1()
        {
            InitializeComponent();
            die.Add(pictureBox1);
            die.Add(pictureBox2);
            die.Add(pictureBox3);
            die.Add(pictureBox4);
            die.Add(pictureBox5);
            dice = new PictureBox[5]
            {
                pictureBox1,
                pictureBox2,
                pictureBox3,
                pictureBox4,
                pictureBox5
            };

            for (int i = 0; i < 5; i++)
            {
                dice[i].Image = imageList1.Images[i];
                i_Array[i] = i;
            }

            lbl_totalscore.Text = "0";
            lbl_rollscore.Text = "0";

        }

        private void btn_roll_Click(object sender, EventArgs e)
        {
            timer1.Interval = 10;

            timer1.Start();
            rolls++;
            lbl_rollscore.Text = rolls.ToString();
            if (rolls == 3)
            {
                btn_roll.Enabled = false;
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < 5; i++)
            {

                i_Array[i] = m_rnd.Next(0, 6);
                dice[i].Image = imageList1.Images[i_Array[i]];
            }


            //int rollScore = 0;
            i_diceTotal = 0;
            diceResults.Clear();
            for (int i = 0; i <= 4; i++)
            {

                diceResults.Add(m_rnd.Next(1, 6));
                int imageNumber = diceResults[i] - 1;
                die[i].Image = this.imageList1.Images[imageNumber];
                i_diceTotal += diceResults[i];
            }
            lbl_totalscore.Text = i_diceTotal.ToString();

            i_rolling++;
            if (i_rolling < 10) return;
            i_rolling++;
            timer1.Stop();
        }

        private void btn_playagain_Click(object sender, EventArgs e)
        {
            rolls = -1;
            dice = new PictureBox[5]
            {
                pictureBox1,
                pictureBox2,
                pictureBox3,
                pictureBox4,
                pictureBox5
            };
            for (int i = 0; i < 5; i++)
            {
                dice[i].Image = imageList1.Images[i];
                i_Array[i] = i;
            }
            rolls++;
            btn_roll.Enabled = true;
            lbl_totalscore.Text = "0";
            lbl_rollscore.Text = "0";

            pictureBox1.BackColor = Color.LimeGreen;
            pictureBox2.BackColor = Color.LimeGreen;
            pictureBox3.BackColor = Color.LimeGreen;
            pictureBox4.BackColor = Color.LimeGreen;
            pictureBox5.BackColor = Color.LimeGreen;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (rolls >= 1)
            {
                pictureBox1.BackColor = Color.Red;
            }
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            if (rolls >= 1)
            {
                pictureBox2.BackColor = Color.Red;
            }
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            if (rolls >= 1)
            {
                pictureBox3.BackColor = Color.Red;
            }
        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {
            if (rolls >= 1)
            {
                pictureBox4.BackColor = Color.Red;
            }
        }

        private void pictureBox5_Click(object sender, EventArgs e)
        {
            if (rolls >= 1)
            {
                pictureBox5.BackColor = Color.Red;
            }
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.