如何使用 foreach 循环生成文本框以在 C# Windows 窗体中显示问题列表



我是Windows表单的新手。

我的程序是显示随机问题供用户回答,我希望当用户按下结束按钮显示问题和用户答案和正确答案时,我的questionanswers应该在var

我创建了一个字符串列表,但似乎无法弄清楚。

namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
int numofquestionleft;
int answer;
int useranswer33;
Timer Clock = new Timer();
List<string> questions = new List<string>();
List<string> answers = new List<string>();
List<string> useranswers= new List<string>();

public Form1(){
}

int[] Rand(int v)
{//random number generater
Random r = new Random();
int a = r.Next(1,v);
int b = r.Next(1,v);
int[] N = {Math.Max(a,b) , Math.Min(a,b)};
return N;

}
void generate()
{
//user choosing the range of numbers that is used in questions
int range = 0;
switch (rangebox1.SelectedIndex)
{ 
case 0:
range = 100;
break;
case 1:
range = 500;
break;
case 2:
range = 1000;
break;
default:
MessageBox.Show("Put a range !");
break;

}
int[] numbers = Rand(range);           
switch (Operationbox1.SelectedIndex)
{ 
case 0:
questionlbl1.Text = string.Format("{0} + {1} =",numbers[0],numbers[1]);
answer = numbers[0] + numbers[1];
answers.Add(answer.ToString());
break;
case 1:
questionlbl1.Text = string.Format("{0} - {1} =" , numbers[0], numbers[1]);
answer = numbers[0] - numbers[1];
answers.Add(answer.ToString());
break;
case 2:
questionlbl1.Text = string.Format("{0} * {1} =" , numbers[0], numbers[1]);
answer = numbers[0] * numbers[1];
answers.Add(answer.ToString());
break;
case 3:
questionlbl1.Text = string.Format("{0} / {1} =" , numbers[0], numbers[1]);
answer = numbers[0] / numbers[1];
answers.Add(answer.ToString());
break;
default:
MessageBox.Show("Please insert a operation");
break;   
}

}
void numberquest()
{//if the number of question left is more than 0 genrate and minues 1 question
if (numofquestionleft > 0)
{
generate();
}
else
{
MessageBox.Show("End of questions, Please press END");
}
numofquestionleft--;

numberofquestionnumber.Text = "Questions left " + numofquestionleft;        
}
private void genbutton1_Click(object sender, EventArgs e)
{ //Button that start the timer and generate the question
numofquestionleft = Convert.ToInt32(numofquest1.Text);
generate();  
}
private void end22_Click(object sender, EventArgs e)
{
foreach (var questionlist in question)
{
// here is my problem what should I put here to display the question list 

}
}
private void button2_Click(object sender, EventArgs e)
{//button that checks if the useranswer is right or wrong
useranswer33 = Convert.ToInt32(useranswerbox1.Text);
useranswers.Add(answer.ToString());
if (useranswer33 == answer)
{
result.Text = "Your answer is correct";
}
else
{
result.Text = "Your answer is Wrong, The correct answer;
}
}
}
}

您可以执行以下操作来动态生成控件以创建测验:

var questions = new string[]
{ 
"This is the question 1",
"This is the question 2",
"This is the question 3",
"This is the question 4"
}
int x = 50;
int y = 10;
int dy = 30;
int width = 250;
foreach ( var question in questions )
{
var textbox = new TextBox();
textbox.Text = question;
textbox.Width = width;
textbox.Location = new Point(x, y);
Controls.Add(textbox);
y += dy;
}

您可以通过根据需要更改和添加控件来适应所需的内容:控件类型、外观、位置、大小、内容等。

此处的控件将添加到窗体本身,但您可以将它们放在面板或任何容器中:

SomePanel.Controls.Add(textbox);

因此,您可以创建带有标签的问题,并为答案添加一个或多个单选框并命名它们。您还可以保留创建的控件,以便以后获取答案:

var texboxesAnswers = new List<TextBox>()
int index = 0;
// ...
foreach ( var question in questions )
{
var label = new Label();
label.Text = question;
label.AutoSize = true;
label.Location = new Point(x, y);
PanelQuestions.Controls.Add(label);
y += dy;
var textbox = new TextBox();
textbox.Text = "";
textbox.Width = width;
textbox.Location = new Point(x, y);
textbox.Name = "TextBoxAnswer" + (index++);
PanelQuestions.Controls.Add(textbox);
texboxesAnswers.Add(textbox);
y += dy + dy / 2;
}

不使用textboxesAnswers您可以得到这样的所有答案:

using System.Linq;
var textboxesAnswers = PanelQuestions.Controls
.OfType<TextBox>()
.Where(n => n.Name.StartsWith("TextBoxAnswer"));

最新更新