2013-02-18 52 views
0

我有一个测试项目,将4个字符串放在列表中,但似乎无法做到这一点。我正在尝试使用for和foreach循环在2个文本框中查看我的列表。把字符串放在列表<string>和输出

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(); 
     } 
    } 
} 
+1

你的问题是什么? – 2013-02-18 02:00:24

+0

不显示列表中所有字符串的循环。下面的答案适用于foreach,但是for循环没有显示任何输出。 – 2013-02-18 02:24:17

回答

1

没有你告诉我们,你的问题是什么,我只能猜测,不是所有的值是你想要文本框。如果我是正确的,你试图做什么是一个字符串列表,你应该使用文本

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]); 
      } 
     } 
    } 
} 

的AppendText通过。如果是这样,没有理由嵌套。这是你想要的吗?

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]); 
     } 
    } 
} 
+0

谢谢你为for循环工作。但是对于foreach循环,它只显示来自textbox1的字符串。试图把一个增量list2 [i],但仍然没有工作 – 2013-02-18 02:14:54

+0

对不起,这是for循环有一个问题,而不是foreach循环。 – 2013-02-18 02:18:05

+0

哪个循环是“最高”的循环? – 2013-02-18 02:23:09