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 中的图片之间建立关联?

更新

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

1 回答 1

2

您可以尝试在 quiz.txt 文件中包含图像索引:

3:酒吧里的foo是什么?
10:如何添加小部件?
4:为什么选择 bar 而不是 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; 
    } 
  } 
于 2012-04-09T13:33:47.697 回答