954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with Picturebox and Timers

I'm creating a simple Slot Machine. one button for the start, and 3 stop buttons on each tiles of Picturebox. my problem is, Every time i clicked each of the stop buttons, the Picturebox won't stop. I need help in which, if i clicked the stop button on a corresponding Picturebox, it will stop and the two will continue to shuffle pictures then if i clicked the other stop button, another will stop and so on. thanks!

here's what i have for now.

namespace SlotMachine
{
    class SlotMac
    {

        private Form f;
        Button btn1 = new Button(); //first stop
        Button btn2 = new Button(); //second stop
        Button btn3 = new Button(); //third stop
        Button btn4 = new Button(); //start
        Timer Clock;    //tick
        Timer Clock1;   //tick
        Timer Clock2;   //tick
        Int32 tick = 0;

        public SlotMac()
        {

            f = new Form();
            f.Text = "Slot Machine";
            //f.Size = new Size(800, 700);
            f.FormBorderStyle = FormBorderStyle.FixedSingle;
            
        }

        PictureBox[] pics = new PictureBox[7];
        PictureBox pb = new PictureBox();

        public void Launch()
        {
            int i = 0;

            Clock = new Timer();
            Clock.Interval = 800;
            Clock.Tick += new EventHandler(Clock_Tick);

            Clock1 = new Timer();
            Clock1.Interval = 900;
            Clock1.Tick += new EventHandler(Clock1_Tick);

            Clock2 = new Timer();
            Clock2.Interval = 1000;
            Clock2.Tick += new EventHandler(Clock2_Tick);

            int x = 50;
            for (i = 0; i < pics.Length; i++)
            {

                pics[i] = new PictureBox();
                pics[i].Image = Image.FromFile(i+".jpg");
                pics[i].SetBounds(x, 100, 100, 100);
                x += 150;
                f.Controls.Add(pics[i]);
            }

            f.SetBounds(10, 20, 500, 500);
                    
            //STOP
            btn1.Location = new Point(50, 250);
            btn1.Height = 40;
            btn1.Width = 100;
            f.Controls.Add(btn1);
            btn1.Text = "STOP";
            this.btn1.Click += new EventHandler(this.btn1_Click);

            //STOP
            btn2.Location = new Point(200, 250);
            btn2.Height = 40;
            btn2.Width = 100;
            btn2.Text = "STOP";
            f.Controls.Add(btn2);
            this.btn2.Click += new EventHandler(this.btn2_Click);

            //STOP
            btn3.Location = new Point(350, 250);
            btn3.Height = 40;
            btn3.Width = 100;
            btn3.Text = "STOP";
            f.Controls.Add(btn3);
            this.btn3.Click += new EventHandler(this.btn3_Click);

            //START
            btn4.Location = new Point(200, 370);
            btn4.Height = 40;
            btn4.Width = 100;
            btn4.Text = "START";
            f.Controls.Add(btn4);
            this.btn4.Click += new EventHandler(btn4_Click);
            f.ShowDialog();

        }

        public void Stop_Click(object sender, EventArgs e)
        {

        }

        public void Clock_Tick(object sender, EventArgs e)
        {

            tick++;
            Random r = new Random();
            pics[0].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
            pics[1].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
            pics[2].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
            pics[3].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
            pics[4].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
            pics[5].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
            pics[6].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
            
        }


        public void Clock1_Tick(object sender, EventArgs e)
        {

            tick++;
            Random r = new Random();
            pics[0].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
            pics[1].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
            pics[2].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
            pics[3].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
            pics[4].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
            pics[5].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
            pics[6].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
        }

        public void Clock2_Tick(object sender, EventArgs e)
        {

            tick++;
            Random r = new Random();
            pics[0].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
            pics[1].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
            pics[2].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
            pics[3].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
            pics[4].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
            pics[5].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
            pics[6].Image = Image.FromFile(r.Next(0, 6) + ".jpg");

        }

        public void btn1_Click(object sender, EventArgs e)
        {
            
            Clock.Stop();
            Clock1.Start();
            Clock2.Start() ;
        }

        public void btn2_Click(object sender, EventArgs e)
        {

            Clock.Start();
            Clock1.Stop();
            Clock2.Start();
        }

        public void btn3_Click(object sender, EventArgs e)
        {

            Clock.Start();
            Clock1.Start(); 
            Clock2.Stop();
        }

        public void btn4_Click(object sender, EventArgs e)
        {
            Clock.Start();
            Clock1.Start();
            Clock2.Start();
        }

        public void Stop()
        {
            btn1.Enabled = true;
            Clock.Stop();

        }
    }
markaroni
Newbie Poster
3 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

Your start button is correct.
But each of the stop buttons should only stop one of the timers.
You do not need to start the timers in your stop buttons!

nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187
 

okay. I also tried to only put Clock, Clock1, Clock2.Stop(); on each buttons but the shuffle of images won't stop either. any ideas on how will I work on this?

markaroni
Newbie Poster
3 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

If you are still having problems with this then please post your current code and I'll take a look; otherwise please mark the thread solved.

nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187
 

Each of the stop buttons are now working. I created three arrays of Picture Box. Thanks.

markaroni
Newbie Poster
3 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: