2011-08-17 139 views
0

我想在單擊所有三個停止按鈕後啓用我的開始按鈕。如何在單擊停止按鈕後啓用開始按鈕

我試圖將按鈕btn4.enabled = false放在(sender == btn3)中,但是如果我第一次點擊那個按鈕,啓動按鈕將被啓用。

三個停止按鈕可以隨機點擊。

這裏是我到目前爲止的代碼:

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!"; 
      } 
     } 
    } 
} 
+0

所以,如果你再次點擊它的startbutton是越來越點擊?你什麼意思?它相當混亂。 – Zenwalker 2011-08-17 10:43:05

+0

爲什麼不只是用一個標誌來檢查點擊了多少個按鈕? – Nayish 2011-08-17 10:43:56

回答

1

您可以檢查是否所有的計時器已經停止,就像這樣:

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; 
} 
1

嘗試此方法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; 
} 
0

,因爲他們被點擊(這將使意義上的),您可以禁用每個按鈕,然後經過他們全部禁用,啓用「開始」按鈕。

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();