Solved

Get all the strings of a stringlist in a textbox.

Posted on 2014-02-06
6
201 Views
Last Modified: 2014-02-06
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
0
Comment
Question by:peterkiers
  • 4
  • 2
6 Comments
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 39840639
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
 
LVL 1

Author Comment

by:peterkiers
ID: 39840649
Hi, thank you for the response. I get: Cannot implicitly convert 'type' to 'bool' at the IF-line

P
0
 
LVL 1

Author Comment

by:peterkiers
ID: 39840654
Oh got it must be ==

Peter
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 1

Author Comment

by:peterkiers
ID: 39840658
Is it possible to display the strings underneath each other then next to each other.

Peter
0
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
ID: 39840671
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
 
LVL 1

Author Comment

by:peterkiers
ID: 39840675
Thanks. Peter
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question