将图像与随机测试的问题相关联

时间:2012-04-05 11:50:49

标签: c#

我正在编写一个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

时会停止

如何在ImageList中将current_question与其图片进行关联?

更新

 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;
        }
      }

1 个答案:

答案 0 :(得分: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。

如果您仍在显示问题和图像索引,则表示您在显示line变量时显示questionText变量。另外,在将图像索引分配给问题时要小心“off by one”错误,因为索引从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; 
    } 
  }