How to put the newline(n) 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[] { "n", "r" },
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}nNot workingnn ", link);
//ListBox1.SelectedItem+= string.Format("{0}nNot workingnn ", link);
}
else
{
// ListBox1.SelectedValue += string.Format("{0}nNot workingnn ", link);
TextBox2.Text += string.Format("{0}n workingnn", link);
}
string[] values = TextBox2.Text.Split(',');
foreach (string value in values)
{
if (value.Trim() == "")
continue;
ListBox1.Items.Add(value.Trim());
}
}
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can use Regex.Split to split up your string to multiple lines like this:
foreach (string s in Regex.Split(TheStringwithNewLines, "n")) ListBox.Items.Add(s);
Method 2
Your code should work fine, I can only suspect that you are not splitting the string properly, are you sure that you have , delimiter in your string , because only then you will see all the items in one line, since split would produce only a single item
Just try the sample code and it will add the items in multiple lines.
string str = "somelink1,somelink2,somelink3";
string[] values = str.Split(',');
foreach (string value in values)
{
if (value.Trim() == "")
continue;
ListBox1.Items.Add(value.Trim());
}
Now if for some reason you end up with string "somelink1 somelink2 somelink3", without the comma as delimiter , you will get a single string in your array and that will appear in a single line.
EDIT: Based on your edited question.
You are adding n and then you are trying to split it on comma your split statement should be:
string[] values = TextBox2.Text.Split(new[] { "n" }, StringSplitOptions.RemoveEmptyEntries);
Method 3
I guess you can use this code

Method 4
Try to see if there is something like Listbox.Items.Row
or
ListBox.Items.Add(value.Trim() + "<BR />");
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0