hitarth
hitarth

Reputation: 317

How to put a \n(new line) inside a list box?

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 working\n\n ", link);
                //ListBox1.SelectedItem+= string.Format("{0}\nNot working\n\n ", link);
            }
            else
            {
               // ListBox1.SelectedValue += string.Format("{0}\nNot working\n\n ", link);
                TextBox2.Text += string.Format("{0}\n working\n\n", link);
            }

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

foreach (string value in values)
{
   if (value.Trim() == "")
       continue;
   ListBox1.Items.Add(value.Trim());    
       }
    }
}

Upvotes: 2

Views: 29880

Answers (4)

Habib
Habib

Reputation: 223422

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);

Upvotes: 1

vikbehal
vikbehal

Reputation: 1524

Try to see if there is something like Listbox.Items.Row

or

ListBox.Items.Add(value.Trim() + "<BR />");

Upvotes: 0

Ikram Shah
Ikram Shah

Reputation: 1246

I guess you can use this code
&#13

Upvotes: 0

Glory Raj
Glory Raj

Reputation: 17701

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); 

Upvotes: 3

Related Questions