2014-01-23 52 views
-1

我正在c#中快速測驗遊戲,並且在播放時我正在努力使標籤(questionLabel)和按鈕(ans1 - ans4)顯示指定的文本按鈕已被點擊,預先感謝您。事件觸發時文本屬性不會改變

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; 
using System.Media; 

namespace WindowsFormsApplication1 
{ 
     public partial class Form1 : Form 
{ 
    int pointCounter = 0; 
    private SoundPlayer _soundPlayer; 
    int questionNr = 1; 

    public Form1() 
    { 
     InitializeComponent(); 
     _soundPlayer = new SoundPlayer("song.wav"); 
    } 

    private void pictureBox1_Click(object sender, EventArgs e) 
    { 
     System.Diagnostics.Process.Start("http://rads.stackoverflow.com/amzn/click/B007AFS0N2"); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     _soundPlayer.PlayLooping(); 
    } 


    private void label1_Click(object sender, EventArgs e) 
    { 

    } 

    private void muteButton_Click(object sender, EventArgs e) 
    { 
     if (muteButton.Text == "Mute") 
     { 

      muteButton.Text = "Unmute"; 
      _soundPlayer.Stop(); 
     } 

     else 
     { 
      muteButton.Text = "Mute"; 
      _soundPlayer.PlayLooping(); 
     } 



    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
      ans1.Visible = true; 
      ans2.Visible = true; 
      ans3.Visible = true; 
      ans4.Visible = true; 
      playButton.Visible = false; 

     while (questionNr >= 2) 
      { 
       if (questionNr == 1) 
       { 
        questionLabel.Text = "What is Chuck's full name?"; 
        ans1.Text = "fushfus"; 
        ans2.Text = "bhfsfs"; 
        ans3.Text = "Chuck"; 
        ans4.Text = "sfhus"; 
       } 
      } 
    } 

    private void ans3_Click(object sender, EventArgs e) 
    { 
     if (questionLabel.Text == "What is Chuck's full name?") 
     { 
      pointCounter++; 
     } 
     else 
     { 

     } 
    } 
     public void PointCounter(){ 
      pointsLabel.Text = pointCounter.ToString(); 
    } 
    } 
} 
+0

questionNr == 1將總是在同時questionNr> = 2環路假。 – venerik

+0

我改變while循環while(questionNr <2),但現在我的程序崩潰 –

+0

爲什麼你需要while循環? – scheien

回答

0

如果button1是播放按鈕,那麼我想你錯了while()循環。

它將永遠不會輸入while(questionNr >= 2),因爲當您創建實例時questionNr爲1。

public partial class Form1 : Form 
{ 
    int pointCounter = 0; 
    private SoundPlayer _soundPlayer; 
    int questionNr = 1; <-- 

    public Form1() { ... } 
} 

變化:

 while (questionNr >= 2) 
     { 
      if (questionNr == 1) 
      { 
       questionLabel.Text = "What is Chuck's full name?"; 
       ans1.Text = "fushfus"; 
       ans2.Text = "bhfsfs"; 
       ans3.Text = "Chuck"; 
       ans4.Text = "sfhus"; 
      } 
     } 

到:

  if (questionNr == 1) 
      { 
       questionLabel.Text = "What is Chuck's full name?"; 
       ans1.Text = "fushfus"; 
       ans2.Text = "bhfsfs"; 
       ans3.Text = "Chuck"; 
       ans4.Text = "sfhus"; 
      } 
相關問題