我有一个测试项目,它将4个字符串放入列表但似乎无法正确执行。我试图使用for和foreach循环在2个文本框中查看我的列表。
private void button1_Click(object sender, EventArgs e)
{
List<string[]> testList2 = new List<string[]>();
string[] text = { textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text };
testList2.Add(text);
textBox5.Text = testList2.Count.ToString();
foreach (string[] list1 in testList2)
{
foreach (string list2 in list1)
{
textBox6.Text = list2.ToString();
}
}
string temp = testList2.ToString();
for (int i = 0; i < testList2.Count; i++)
{
for (int j = 0; j < i; j++)
{
textBox7.Text = testList2[j].ToString();
}
}
}
答案 0 :(得分:1)
如果没有您告诉我们您的问题是什么我只能猜测并非所有值都在您想要的文本框中。您应该使用AppendText而不是Text
private void button1_Click(object sender, EventArgs e)
{
List<string[]> testList2 = new List<string[]>();
String[] text = { textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text };
testList2.Add(text);
textBox5.Text = testList2.Count.ToString();
foreach (string[] list1 in testList2)
{
foreach (string list2 in list1)
{
textBox6.AppendText(list2);
}
}
for (int i = 0; i < testList2.Count; i++)
{
for (int j = 0; j < i; j++)
{
textBox7.AppendText(testList2[i][j]);
}
}
}
}
如果我是正确的,你尝试制作的是一个字符串列表。如果是这样,就没有理由进行嵌套。这是你想要的吗?
private void button1_Click(object sender, EventArgs e)
{
List<String> testList2= new List<String>();
testList2.Add(textBox1.Text);
testList2.Add(textBox2.Text);
testList2.Add(textBox3.Text);
testList2.Add(textBox4.Text);
textBox5.Text = testList2.Count.ToString();
foreach (String val in testList2)
{
textBox6.AppendText(val);
}
for (int i = 0; i < testList2.Count; i++)
{
textBox7.AppendText(testList2[i]);
}
}
}