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