Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

c# - How to put a (new line) inside a list box?

How to put the newline( ) in list box .selected item? Here I have code it generates the all links but I want to have all those link in listbox, these code is working but the links are not coming in the new line all comes in single line and my code is:

var links = TextBox1.Text.Split(new string[] { "
", "
" }, 
StringSplitOptions.RemoveEmptyEntries);

  foreach (var link in links)
        {
            if (!IsLinkWorking(link))
            {
                //Here you can show the error. You don't specify how you want to show it.
                TextBox2.ForeColor = System.Drawing.Color.Green; 
               TextBox2.Text += string.Format("{0}
Not working

 ", link);
                //ListBox1.SelectedItem+= string.Format("{0}
Not working

 ", link);
            }
            else
            {
               // ListBox1.SelectedValue += string.Format("{0}
Not working

 ", link);
                TextBox2.Text += string.Format("{0}
 working

", link);
            }

string[] values = TextBox2.Text.Split(',');

foreach (string value in values)
{
   if (value.Trim() == "")
       continue;
   ListBox1.Items.Add(value.Trim());    
       }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use Regex.Split to split up your string to multiple lines like this:

foreach (string s in Regex.Split(TheStringwithNewLines, "
"))
ListBox.Items.Add(s); 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...