2012-04-05 66 views
2

我正在編寫一個C#程序來顯示圖像問題的隨機測試。將圖像與隨機測試問題關聯起來

該測試有10個問題。我也有10個圖像添加到ImageList中。我的問題是隨機選擇的,以顯示我解決的每個測驗。我想每個問題都有它的圖片。

我有,我從文件加載問題的集合:

Collection<question> questions = new Collection<question>(); 
StreamReader sr = new StreamReader("quiz.txt"); 
while (!sr.EndOfStream) 
{ 
    question i = new question(); 
    i.text = sr.ReadLine(); 
    questions.Add(i); 
} 
sr.Close(); 

Random r = new Random(); 
int x = r.Next(questions.Count); 

我說從工具箱中ImageList控制。然後我使用圖像集合編輯器將圖像添加到它。因爲我使用的代碼:

pictureBox1.Image = imageList1.Images[a]; 

a > imageList1.Images.Count

我怎麼可能讓current_question從ImageList中的圖像之間的相關性這會停止?

UPDATE

public class question 
{ 
public bool displayed = false; 
public string text, answer1, answer2; 
} 

private void button1_Click_1(object sender, EventArgs e) 
{ 
     string line = questions[current_question].text; 
     int delimiter = line.IndexOf(':'); 
     int imageIndex = int.Parse(line.Substring(0, delimiter)); 
     string questionText=line.Substring(delimiter + 1); 
     pictureBox1.Image = imageList1.Images[imageIndex];//I still have problems with  
                  //images 
     if (nr > questions.Count) 
           { 
            button1.Enabled = false; 
            } 
     else 
     { 
      Random r = new Random(); 
      int x; 
      do { x = r.Next(questions.Count); } 
        while (questions[x].displayed == true); 
      textBox1.Text = questionText;// now it doesn't appear the index;thank you 
      radioButton1.Text = questions[x].answer1; // is not from the current 
                 // question 
      radioButton2.Text = questions[x].answer2;// is not from the current 
                // question 
      questions[x].displayed= true; 
      current_question = x; 
     } 
     } 
+0

開始你如何填寫你的'questions'收集,以及如何你填寫你的'ImageList'? – Justin 2012-04-05 12:01:41

+2

在您的文本文件中,您的問題與您的圖像如何包含在ImageList中一一對應地排列?比如,第一個問題是第一個圖像,第二個問題是第二個圖像等等?如果不是,你如何聯繫他們? – 2012-04-09 02:00:22

回答

2

你可以嘗試,包括在你的quiz.txt文件中的圖像索引:

3:什麼是在酒吧foo的?
10:如何添加小部件?
4:爲什麼在foo上挑一個酒吧?

然後,當你拿起你的隨機問題,你可以提取的圖像索引和問題的文字在你的程序中的點:

int x = r.Next(questions.Count); 
// intrebari is a variable of class 'question' 
string line = intrebari[x].text; 
// find the colon 
int delimiter = line.IndexOf(':'); 
// the image index is left of the colon 
int imageIndex = int.Parse(line.Substring(0, delimiter)); 
// and the question itself is right of the colon 
string questionText = line.Substring(delimiter + 1); 
// set the image in the picture box 
pictureBox1.Image = imageList1.Images[imageIndex]; 

那麼接下來questionText是你顯示的字符串和imageIndex你可以用它來選擇正確的圖像:imageList1.Images[imageIndex]分配給你的PictureBox。

如果您仍然顯示問題和圖像索引,這意味着當您應該顯示questionText變量時,您正在顯示line變量。此外,分配圖像索引你的問題時,「由一休」錯誤提防的,因爲索引爲0

編輯

private void button1_Click_1(object sender, EventArgs e) 
{ 
    if (nr > questions.Count) 
    { 
     button1.Enabled = false; 
    } 
    else 
    { 
     Random r = new Random(); 
     int x; 
     do { x = r.Next(questions.Count); } 
       while (questions[x].displayed == true); 

     string line = questions[x].text; 
     int delimiter = line.IndexOf(':'); 
     int imageIndex = int.Parse(line.Substring(0, delimiter)); 
     string questionText=line.Substring(delimiter + 1); 
     pictureBox1.Image = imageList1.Images[imageIndex];//I still have problems with   

     textBox1.Text = questionText;// now it doesn't appear the index;thank you 
     radioButton1.Text = questions[x].answer1; // is not from the current 
                // question 
     radioButton2.Text = questions[x].answer2;// is not from the current 
               // question 
     questions[x].displayed= true; 
     current_question = x; 
    } 
    } 
+0

@Bodgan,你需要點擊你的問題下的'編輯'按鈕,幷包括你的相關代碼,因爲我無法弄清楚當我看不到你的代碼時出了什麼問題。 – 2012-04-14 17:06:36

+0

@Brad_Rem,我使用了'編輯'按鈕。謝謝。 – Bogdan 2012-04-15 09:18:08

+0

@ Bogdan,看我的編輯。問題在於你的current_question和do-while循環。你有current_question的圖像,這是最後一個問題,你的do-while循環提出了一個新的問題。使用調試器觀察變量逐行執行代碼會很有幫助!我希望這一切都能回答你原來的問題。 – 2012-04-15 11:17:09