Boogie Boogie - 28 days ago 15
C# Question

How to stop shuffle of images using timers in C#

I'm creating a simple slot machine. One button for the start, and three stop buttons on each tile of a Picturebox. My problem is every time I click each of the stop buttons, the Picturebox won't stop.

I need help in which, if I click 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.

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 = 1000;
Clock.Tick += new EventHandler(Clock_Tick);

Clock1 = new Timer();
Clock1.Interval = 1000;
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.MyButtonClick);

// 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.MyButtonClick);

// 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.MyButtonClick);

// 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 MyButtonClick(object sender, EventArgs e)
{
// I am having troubles in this part
if (sender == btn1)
{
Clock.Stop();

}
if (sender == btn2)
{
Clock1.Stop();
}
if (sender == btn3)
{
Clock2.Stop();
}
}

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

Answer

I think you need to create a separate PictureBox arrays for each clock. Otherwise, the PictureBox for what is getting shown will continually reflect that changes made by the other two timers.

Basically, if they are all based on the same array, then when the other clocks change the array, it doesn't matter if the first clock changes it or not; it still gets changed.

Comments