Get all the strings of a stringlist in a textbox.

Hi, I have this code below to display all the string of a stringlist in a textbox.
But even thou there are more strings in the stringlist I only get one string
displayed in the textbox. What do I do wrong?

private List<string> opened = new List<string>();

private void button1_click(object sender, EventArgs e)
{
       foreach (string mylist in opened)
       {
        testking.Text=mylist;
       }
}

Open in new window


Greetings, Peter Kiers
LVL 1
peterkiersAsked:
Who is Participating?
 
Paul JacksonConnect With a Mentor Software EngineerCommented:
You should be able to use Environment.NewLine instead of a space to separate the values:

private List<string> opened = new List<string>();

private void button1_click(object sender, EventArgs e)
{
       foreach (string mylist in opened)
       {
            if (testking.Text == string.Empty)
            {
                 testking.Text=mylist;
            }
            else
            {
                 testking.Text = testKing.Text + Environment.NewLine + mylist;
            }
       }
} 

Open in new window

0
 
Paul JacksonSoftware EngineerCommented:
Each time you are looping you are overwriting the Textbox value.
try something like:
private List<string> opened = new List<string>();

private void button1_click(object sender, EventArgs e)
{
       foreach (string mylist in opened)
       {
            if (testking.Text = string.Empty)
            {
                 testking.Text=mylist;
            }
            else
            {
                 testking.Text = testKing.Text + " " + mylist;
            }
       }
}

Open in new window

0
 
peterkiersAuthor Commented:
Hi, thank you for the response. I get: Cannot implicitly convert 'type' to 'bool' at the IF-line

P
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
peterkiersAuthor Commented:
Oh got it must be ==

Peter
0
 
peterkiersAuthor Commented:
Is it possible to display the strings underneath each other then next to each other.

Peter
0
 
peterkiersAuthor Commented:
Thanks. Peter
0
All Courses

From novice to tech pro — start learning today.