2012-04-05 27 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