2

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());    
       }
    }
}
hitarth
  • 317
  • 4
  • 5
  • 19
  • You are not splitting the string properly, What is the value inside your `TextBox2.Text`, see my [new answer](http://stackoverflow.com/a/15354795/961113) – Habib Mar 12 '13 at 06:32

4 Answers4

3

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); 
Glory Raj
  • 17,397
  • 27
  • 100
  • 203
1

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);
Habib
  • 219,104
  • 29
  • 407
  • 436
  • bro its working but i want links it will display somelink1,somelink2,somelink3,but i want links and links are generated so i can't manually right so can you guide me – hitarth Mar 12 '13 at 06:47
  • @hitarth, there should be a way to split the string, if you want to do it dynamically , then there should be a predefined delimiter for your string. – Habib Mar 12 '13 at 06:49
  • see please see my edited code .from that you can guide me what string should i pass – hitarth Mar 12 '13 at 06:54
  • @hitarth, what is in `TextBox2.Text` ? – Habib Mar 12 '13 at 07:01
  • bro all the links i generated it comes in textbox2 when i click on convert i want that links in listbox1 ,i am getting those links but in single line – hitarth Mar 12 '13 at 07:02
  • @hitarth, check the edited part of the answer, you are splitting on the wrong delimiter – Habib Mar 12 '13 at 07:06
0

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

or

ListBox.Items.Add(value.Trim() + "<BR />");
शेखर
  • 17,412
  • 13
  • 61
  • 117
vikbehal
  • 1,486
  • 3
  • 21
  • 48
0

I guess you can use this code
&#13

Ikram Shah
  • 1,206
  • 1
  • 11
  • 12