2010-10-01 88 views
1

我正在自己做一本書的實驗,我做了一個鯊魚比賽的應用程序。有一個單選按鈕可以動態更新右側的標籤,以及一個真正開始比賽的按鈕。一切都用於工作,然後我重命名了一些東西,現在沒有任何工作。爲什麼我的C#應用​​程序中的按鈕不工作?

截圖應用:

image http://cl.ly/f08f4e22761464e0c2f3/content

Form類:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace project1 
{ 
    public partial class Game : Form 
    { 
     private Shark[] sharks; 
     private Guy[] guys; 
     private Guy selectedGuy; 

     public Game() 
     { 
      InitializeComponent(); 

      Random moreRandom = new Random(); 

      int start = myTrack.Location.X; 
      int finish = myTrack.Width - 65; 

      sharks = new Shark[4] 
       { 
        new Shark() {myRandom = moreRandom, myPictureBox = myShark1, myPBStart = start, trackLength = finish}, 
        new Shark() {myRandom = moreRandom, myPictureBox = myShark2, myPBStart = start, trackLength = finish}, 
        new Shark() {myRandom = moreRandom, myPictureBox = myShark3, myPBStart = start, trackLength = finish}, 
        new Shark() {myRandom = moreRandom, myPictureBox = myShark4, myPBStart = start, trackLength = finish} 
       }; 

      guys = new Guy[3] 
       { 
        new Guy() {myName="Joe", cash=50, myRadioButton=rbGuy1, myLabel=labelBet1}, 
        new Guy() {myName="Bob", cash=75, myRadioButton=rbGuy2, myLabel=labelBet2}, 
        new Guy() {myName="Al", cash=45, myRadioButton=rbGuy3, myLabel=labelBet3} 
       }; 

      selectedGuy = guys[0]; 
      rbGuy1.Tag = guys[0]; 
      rbGuy2.Tag = guys[1]; 
      rbGuy3.Tag = guys[2];    

      updateGui(); 
     } 

     private void myChanged(object sender, EventArgs e) 
     { 
      selectedGuy = getSelectedGuy(sender); 
      betterLabel.Text = selectedGuy.myName; 
     } 

     private void betAmountValue(object sender, EventArgs e) 
     { 
      updateMin(); 
     } 

     private void Bet_Click(object sender, EventArgs e) 
     { 
      int bet = (int) betAmount.Value; 
      int myFish = (int) sharkNumber.Value; 
      selectedGuy.placeBet(bet, myFish); 
      updateGui(); 
     } 

     private void raceBtn_Click(object sender, EventArgs e) 
     { 
      betBtn.Enabled = false; 

      bool noWinner = true; 
      while(noWinner) 
      { 
       for (int dogFish = 0; dogFish < sharks.Length; dogFish++) 
       { 
        Application.DoEvents(); 
        if(sharks[dogFish].Swim()) 
        { 
         showWinner(dogFish); 
         collectBets(dogFish); 
         noWinner = false; 
        } 
       } 
      } 

      updateGui(); 

      betBtn.Enabled = true; 
     } 

     private void showWinner(int fish) 
     { 
      MessageBox.Show(string.Format("Winner Winner People Dinner! \nShark {0} won!", fish + 1)); 
     } 

     private void collectBets(int fish) 
     { 
      for (int guyNumber = 0; guyNumber < guys.Length; guyNumber++) 
      { 
       guys[guyNumber].collect(fish + 1); 
       guys[guyNumber].resetBet(); 
      } 
     } 

     private void updateMin() 
     { 
      minBetLabel.Text = string.Format("Minimum bet: 5 bucks", betAmount.Value); 
     } 

     private Guy getSelectedGuy(object sender) 
     { 
      RadioButton rb = (RadioButton)sender; 
      return (Guy)rb.Tag; 
     } 

     private void updateGui() 
     { 
      for (int guyNumber = 0; guyNumber < guys.Length; guyNumber++) 
      { 
       guys[guyNumber].updateLabels(); 
      } 

      for (int fish = 0; fish < sharks.Length; fish++) 
      { 
       sharks[fish].startPosition(); 
      } 

      updateMin(); 
     } 
    } 
} 

鯊魚類:

using System; 
using System.Drawing; 
using System.Threading; 
using System.Windows.Forms; 

namespace project1 
{ 
    public class Shark 
    { 
     public int myPBStart; // Where the PictureBox starts 
     public int trackLength; // How long the racetrack is 
     public PictureBox myPictureBox = null; // The PictureBox object 
     public int location = 0; // My location on the racetrack 
     public Random myRandom; // An instance of Random 

     public Shark() 
     { 
      location = 0; 
      myPictureBox = new PictureBox(); 
      myRandom = new Random(); 
      trackLength = 100; 
      myPBStart = 0; 
     } 

     public bool Swim() 
     { 
      int distance = myRandom.Next(1, 4); 
      location += distance; 

      movePB(distance); 

      return location > trackLength; 
     } 

     private void movePB(int distance) 
     { 
      Point p = myPictureBox.Location; 
      p.X += distance; 
      myPictureBox.Location = p; 
     } 

     public void startPosition() 
     { 
      location = myPBStart; 

      Point p = myPictureBox.Location; 
      p.X = location; 
      myPictureBox.Location = p; 
     } 
    } 
} 

,如果需要,我可以提供更多的資源,但這是主它的要點。

+0

如何查看已編輯的內容?我認爲它沒有被修復?我仍然在Visual Studio中使用它。我很抱歉,我是這種語言的新手。 – eightonrose 2010-10-01 02:01:13

+0

你重命名了什麼? – D0cNet 2010-10-01 02:00:55

+0

看起來你改名 「private void Bet_Click(object sender,EventArgs e)」。 將它重命名爲 「private void betBtn_Click(object sender,EventArgs e)」。 並確保事件鏈接到按鈕。 – D0cNet 2010-10-01 02:07:14

回答

2

使用Visual Studio,請確保以下幾點:

1)對於每一個單選按鈕,驗證CheckedChanged事件被掛接到myChanged功能。
2)確認「投注」Button.Click事件掛鉤到您的Bet_Click函數。
3)驗證「Race!」 Button.Click事件掛鉤到您的raceBtn_Click功能。

重命名事物的安全方法是右鍵單擊變量名稱Refactor重命名。這將確保對變量的任何引用都被正確地更名

+0

+1重構>重命名是要走的路。 – DevDemon 2010-10-01 02:15:17

+0

如何驗證?這似乎可能實際上是問題所在。例如,每當我雙擊「設計器」模式中的按鈕時,通常應該將我帶到正確點擊時分配給它的類中?當我雙擊任何這些按鈕時,它會嘗試創建一個新的raceBtn_Click_1函數,而不是帶我參加raceBtn_Click。有沒有辦法將它們聯繫起來? – eightonrose 2010-10-01 02:16:00

+0

您可以關閉VS並將其打開,然後單擊raceBtn的事件部分,輸入正確的事件raceBtn_Click。這應該把它掛起來。 – D0cNet 2010-10-01 02:23:43

1

確保控件上的事件仍然連接到代碼中正確的事件處理程序。有時候,當你重命名的東西,這個鏈接可能會被打破。

+0

。沒有編譯器錯誤,我相信一切都正確鏈接。但是,我認爲for循環有問題。 – eightonrose 2010-10-01 02:09:05

2

當您重命名它們時,您可能是通過編輯代碼而不是通過更改控件屬性來完成的。

VS中的Winforms設計器在後臺創建了用於連接事件的代碼。此代碼使用控件名稱。尋找名爲formname_designer.cs的文件。請注意,有些行仍然有舊的控件名稱。您可以更改此代碼

這就是爲什麼當您開始時控制好名字的好習慣。

+0

它更改了我的Shark和Form類中控件的名稱,但是它可能並未在設計器類中更改它們。我現在就去檢查一下,謝謝! – eightonrose 2010-10-01 02:06:27

+0

不,不是。還是)感謝你的建議!一切似乎都匹配了。它看起來可能是for循環的問題。 「raceBtn」並不像啓動Swim一樣。 – eightonrose 2010-10-01 02:12:05

相關問題