2012-08-09 170 views
1

我是C#的全新品牌。我多年前在大學學習Java,但顯然沒有很好地學習它!現在我非常熱衷於從頭開始學習C#。我對絕對的基本知識有一個體面的想法,但現在已經變得不那麼敏感了。觸發按鈕單擊

我正在使用WPF構建一個相對簡單的測驗「遊戲」應用程序。用戶點擊「加載測驗」按鈕,加載包含問題和答案的文本文件。然後用戶單擊「下一個問題」,它將第一個問題和四個多個選項加載到文本框中。這一切似乎都工作正常。

然後用戶點擊對應於正確答案的按鈕1/2/3/4。當他們這樣做時,如果選擇了正確答案,我希望「正確!完成」文本顯示在tResult中,如果答案不正確,則顯示其他文本。

但是,單擊按鈕不會立即採取行動,直到單擊bNextQ_Click,最後在tResult中顯示「Sorry,that is incorrect」。

看來布爾標誌仍然是假的,所以沒有識別點擊事件。

請注意:我意識到我的代碼會很混亂,我相信我正在以糟糕和低效率的方式執行此操作。任何建議感激地接受!

總之,這裏是代碼,任何反饋是大加讚賞:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using System.IO; 

namespace QuizGame 
{ 
/// <summary> 
/// Interaction logic for MainWindow.xaml 
/// </summary> 
public partial class MainWindow : Window 
{ 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private bool button1Clicked = false; 
    private bool button2Clicked = false; 
    private bool button3Clicked = false; 
    private bool button4Clicked = false; 

    private int score = 0; 

    private void txtQuestion_TextChanged() 
    { 
     throw new NotImplementedException(); 
    } 

    private void bNextQ_Click(object sender, RoutedEventArgs e) 
    { 
     questionIteration(); 

    } 

    private void numQuestions_TextChanged(object sender, TextChangedEventArgs e) 
    { 

    } 

    private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     button1Clicked = true; 
    } 

    private void button2_Click(object sender, RoutedEventArgs e) 
    { 
     button2Clicked = true; 
    } 

    private void button3_Click(object sender, RoutedEventArgs e) 
    { 
     button3Clicked = true; 
    } 

    private void button4_Click(object sender, RoutedEventArgs e) 
    { 
     button4Clicked = true; 
    } 

    private void txtQuestion_TextChanged(object sender, TextChangedEventArgs e) 

    { 
     //txtQuestion.Text = splitted[]; 

    } 

    private void button_Load_Quiz_Click(object sender, RoutedEventArgs e) 
    { 
     int lineCount = File.ReadLines(@"C:\\Users\Morgan\Documents\Visual Studio 2010\Projects\QuizGame\QuizGame\Quiz.txt").Count(); 

     numQuestions.Text = lineCount.ToString(); 

     txtQuestion.Text = "Press 'Next Question' to start."; 
    } 

    private void questionIteration() 
    { 


     int i = 0; 

     string[] questionArray = File.ReadAllLines(@"C:\\Users\Morgan\Documents\Visual Studio 2010\Projects\QuizGame\QuizGame\Quiz.txt"); 

     foreach (string qArrays in questionArray) 
     { 


      string question = questionArray[i]; 

      string[] splitted = question.Split('|'); 

      txtQuestion.Text = splitted[0]; 
      textBox1.Text = splitted[1]; 
      textBox2.Text = splitted[2]; 
      textBox3.Text = splitted[3]; 
      textBox4.Text = splitted[4]; 

      string correct = splitted[5]; 

      if (button1Clicked == true) 
      { 
       if (correct == textBox1.Text) 
       { 
        score++; 

        tResult.Text = "Correct! Well done."; 
       } 
       else tResult.Text = "Sorry, that is incorrect."; 

      } 

      if (button2Clicked == true) 
      { 
       if (correct == textBox2.Text) 
       { 
        score++; 

        tResult.Text = "Correct! Well done."; 
       } 
       else tResult.Text = "Sorry, that is incorrect."; 

      } 


      if (button3Clicked == true) 
      { 
       if (correct == textBox3.Text) 
       { 
        score++; 

        tResult.Text = "Correct! Well done."; 
       } 
       else tResult.Text = "Sorry, that is incorrect."; 

      } 


      if (button4Clicked == true) 
      { 
       if (correct == textBox4.Text) 
       { 
        score++; 

        tResult.Text = "Correct! Well done."; 
       } 
       else tResult.Text = "Sorry, that is incorrect."; 

      } 



      i++; 
      button1Clicked = false; 
      button2Clicked = false; 
      button3Clicked = false; 
      button4Clicked = false; 
     } 

    } 

} 
} 
+1

問題應該是具體的。你是否期待我們編寫你的程序? – Prabhavith 2012-08-09 11:36:27

+1

C#的全新品牌?哈哈哈哈。 – 2012-08-09 11:47:45

+0

更嚴肅的說明。不要馬上去找WPF。去控制檯或WinForms應用程序。 – 2012-08-09 11:48:36

回答

1

點擊一個按鈕,使得沒有立即採取行動,bNextQ_Click點擊

那之前,因爲你已經編寫了「行動」部分,不是馬上,而是在questionIteration()函數內,當點擊下一個問題的按鈕時,這顯然會觸發。爲了解決這個問題,就把這部分代碼在一個單獨的函數,並調用它每一個答案按鈕被點擊時間:

if (button1Clicked == true) 
     { 
      if (correct == textBox1.Text) 
      { 
       score++; 

       tResult.Text = "Correct! Well done."; 
      } 
      else tResult.Text = "Sorry, that is incorrect."; 

     } 

     if (button2Clicked == true) 
     { 
      if (correct == textBox2.Text) 
      { 
       score++; 

       tResult.Text = "Correct! Well done."; 
      } 
      else tResult.Text = "Sorry, that is incorrect."; 

     } 


     if (button3Clicked == true) 
     { 
      if (correct == textBox3.Text) 
      { 
       score++; 

       tResult.Text = "Correct! Well done."; 
      } 
      else tResult.Text = "Sorry, that is incorrect."; 

     } 


     if (button4Clicked == true) 
     { 
      if (correct == textBox4.Text) 
      { 
       score++; 

       tResult.Text = "Correct! Well done."; 
      } 
      else tResult.Text = "Sorry, that is incorrect."; 

     } 

調用此函數您已設置後的布爾標誌buttonxClicked等

0

但是,單擊按鈕不會立即採取行動,直到單擊bNextQ_Click,最後在tResult中顯示「Sorry,that is incorrect」。

這是因爲代碼是這樣寫的。沒有錯誤或什麼,它的設計缺陷。你的按鈕(1-4)點擊,實際上只是設置標誌。這就是爲什麼沒有發生。 Next按鈕實際上運行設置一些消息的代碼。您必須更仔細地考慮解決方案,瞭解發生的情況和順序。現在你的代碼做這樣的事情:

1用戶點擊加載按鈕 - 測驗加載

2用戶點擊1-4 - 標誌設置

3用戶點擊下一個 - 問題是加載再次(什麼爲?)和foreach問題(爲什麼,如果用戶回答一個問題):

3A答案設置(再次 - 爲什麼?)

3B檢查用戶是否得到了正確的答案

3C復位標誌

3D檢查下一個問題(與resseted標誌...)

應該更多這樣的:

1個用戶點擊負載布通 - 測驗被加載時,顯示一條消息,其準備

2用戶點擊下一個 - 負載下一個問題

3用戶點擊1-4 - 有檢查,如果答案是正確的,消息顯示

4用戶點擊下一個...

想想你的解決方案,寫下一些秩序,一些「算法「在一張紙和代碼上。

相關問題