How to enable a start button after clicking stop buttons
I want to enable my start button after clicking all the three stop buttons.
I tried to place the button btn4.enabled = false
inside the (sender == btn3
), but the start button will be enabled if I first clicked on that button.
The three stop buttons can be clicked in random order.
Here's my code so far:
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;
Label tb = new Label();
int[] nNum = new int[3];
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[] pics1 = new PictureBox[7];
PictureBox[] pics2 = new PictureBox[7];
//PictureBox pb = new PictureBox();
public void Launch()
{
Random r = new Random();
int i = 0;
//int x = 0;
//int x = 50;
tb.Location = new Point(205,20);
f.Controls.Add(tb);
Clock = new Timer();
Clock.Interval = 800;
Clock.Tick += new EventHandler(Clock_Tick);
Clock1 = new Timer();
Clock1.Interval = 800;
Clock1.Tick += new EventHandler(Clock1_Tick);
Clock2 = new Timer();
Clock2.Interval = 800;
Clock2.Tick += new EventHandler(Clock2_Tick);
for (i = 0; i < pics.Length; i++)
{
pics[i] = new PictureBox();
pics[0].Image = Image.FromFile(i + ".jpg");
pics[i].SetBounds(50, 100, 100, 100);
//x += 150;
f.Controls.Add(pics[i]);
}
for (i = 0; i < pics1.Length; i++)
{
pics1[i] = new PictureBox();
pics1[i].Image = Image.FromFile(i + ".jpg");
pics1[i].SetBounds(200, 100, 100, 100);
//x += 50;
f.Controls.Add(pics1[i]);
}
for (i = 0; i < pics2.Length; i++)
{
pics2[i] = new PictureBox();
pics2[i].Image = Image.FromFile(i + ".jpg");
pics2[i].SetBounds(350, 100, 100, 100);
//x += 50;
f.Controls.Add(pics2[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 Clock_Tick(object sender, EventArgs e)
{
tick++;
Random r = new Random();
nNum[0] = r.Next(0, 6);
for (int i = 0; i < pics.Length; i++)
{
pics[0].Image = Image.FromFile(nNum[0] + ".jpg");
pics[1].Image = Image.FromFile(nNum[0] + ".jpg");
pics[2].Image = Image.FromFile(nNum[0] + ".jpg");
pics[3].Image = Image.FromFile(nNum[0] + ".jpg");
pics[4].Image = Image.FromFile(nNum[0] + ".jpg");
pics[5].Image = Image.FromFile(nNum[0] + ".jpg");
pics[6].Image = Image.FromFile(nNum[0] + ".jpg");
}
}
public void Clock1_Tick(object sender, EventArgs e)
{
tick++;
Random r = new Random();
nNum[1] = r.Next(0, 6);
for (int i = 0; i < pics.Length; i++)
{
pics1[0].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[1].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[2].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[3].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[4].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[5].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[6].Image = Image.FromFile(nNum[1] + ".jpg");
}
}
public void Clock2_Tick(object sender, EventArgs e)
{
tick++;
Random r = new Random();
nNum[2] = r.Next(0, 6);
for (int i = 0; i < pics.Length; i++)
{
pics2[0].Image = Image.FromFile(nNum[2] + ".jpg");
pics1[1].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[2].Image = Image.FromFile(nNum[1] + ".jpg");
开发者_开发问答 pics1[3].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[4].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[5].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[6].Image = Image.FromFile(nNum[1] + ".jpg");
}
}
public void MyButtonClick(object sender, EventArgs e)
{
if (sender == btn1)
{
Clock.Stop();
}
if (sender == btn2)
{
Clock1.Stop();
}
if (sender == btn3)
{
Clock2.Stop();
}
Finish();
}
public void btn4_Click(object sender, EventArgs e)
{
Clock.Start();
Clock1.Start();
Clock2.Start();
btn4.Enabled = false;
}
public void Finish()
{
if (nNum[0] == nNum[1] && nNum[0] == nNum[2])
{
this.tb.Text = "Congratulations!";
}
}
}
}
You can check if all timers already stopped, like this:
public void MyButtonClick(object sender, EventArgs e)
{
if (sender == btn1)
{
Clock.Stop();
}
if (sender == btn2)
{
Clock1.Stop();
}
if (sender == btn3)
{
Clock2.Stop();
}
Finish();
if(!Clock.Enabled && !Clock1.Enabled && !Clock2.Enabled)
btn4.Enabled = true;
}
Try this for method MyButtonClick:
public void MyButtonClick(object sender, EventArgs e)
{
if (sender == btn1)
{
Clock.Stop();
}
if (sender == btn2)
{
Clock1.Stop();
}
if (sender == btn3)
{
Clock2.Stop();
}
Finish();
if (!Clock.Enabled && !Clock1.Enabled && !Clock2.Enabled) btn4.Enabled = true;
}
You could disable each button as they are clicked (which would make sense) and then after they are all disabled, enable the Start button.
if (sender == btn1)
{
Clock.Stop();
btn1.Enabled = false; // disable the buttons after they are clicked.
}
if (sender == btn2)
{
Clock1.Stop();
btn2.Enabled = false;
}
if (sender == btn3)
{
Clock2.Stop();
btn3.Enabled = false;
}
// If all buttons are disabled then enable the Start button.
if (!btn1.Enabled && !btn2.Enabled && !btn3.Enabled)
{
btn4.Enabled = true;
}
Finish();
精彩评论