0

I have a test project that puts 4 strings to a list but can't seem to do it right. I am trying to view my list in 2 textboxes using the for and foreach loops.

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
  • the loops where not showing all the string inside the list. The answer below worked for the foreach but the for looped didn't show any output. Commented Feb 18, 2013 at 2:24

1 Answer 1

1

Without you telling us what your problem is i can only guess that not all values are in textbox that you want. You should use AppendText instead of 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]);
            }
        }
    }
}

If I am correct what you try to make is a List of strings. If that is so, there is no reason for nesting. Is this what you want?

 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]);
        }
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

Thank you it worked for the for loop. But for the foreach loop it only shows the string from textbox1. Tried to put an increment on list2[i] but still didn't work
Sorry it was The for loop that is having a problem not the foreach loop.
Which loop is the "top" one?
The for loop doesn't output anything sir.
First of all I'm not sure what you're trying to achieve. Second of all, in your for loop you only access first level, you have an array inside a List
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.