如何从文本文件和字符串中读取它分成4个单选按钮? (WPF)

时间:2017-01-03 17:31:38

标签: c# .net wpf

我有一个文本文件,其中包含一个问答测验。

m1 | mcq | Which of the following is an animal | table#cat#keyboard#water
m2 | mcq | which of the following is a programming language |C++#water#Air#Phone
m3 | mcq | which is the prime number | 3#4#8#10 

到目前为止我的进展:

string[] lines = File.ReadAllLines(path);
string[] line = lines[0].Split( new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries );
string QuizQuestion = line[2].Trim();
string[] QuestionChoice = line[3].Split(new string[] { "#" }, StringSplitOptions.RemoveEmptyEntries);
textBox.Text = QuizQuestion;
foreach(?)
{
}

我试图使用foreach循环使我的单选按钮像答案选择,但我不知道如何继续。我已经在我的单选按钮内插入了标签和文本框,以尝试哪一个成功但我失败了

1 个答案:

答案 0 :(得分:0)

如果您总是有4个选择,可以在表单中添加4个单选按钮,将它们放在一个列表中,然后执行以下操作:

string[] QuestionChoice = line[3].Split(new string[] { "#" }, StringSplitOptions.RemoveEmptyEntries);

List<RadioButton> radioButtonList = new List<RadioButton>();

radioButtonList.Add(radioButton1);
radioButtonList.Add(radioButton2);
radioButtonList.Add(radioButton3);
radioButtonList.Add(radioButton4);

for ( int i = 0; i < radioButtonList.Count; i++ )
{
    radioButtonList[i].Content = QuestionChoice[i];
}