Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: c# add new line to listbox  (Read 18211 times)

0 Members and 1 Guest are viewing this topic.

DaftHacker

    Topic Starter


    Rookie

  • I am Daft
    • Yes
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 8
c# add new line to listbox
« on: March 25, 2013, 04:36:50 PM »
What my program does and is download a string from and uploaded file. I need to add a new line to the listbox for every line of string in the downloaded string.

so for example

downloaded string =
firstlineofstring
secondlineofstring

I need to add
listbox1.items.add(downloaded string); //add line 1 of string,  add line 2 of string,  add any other lines

if I just do listbox1.items.add(download string); it will wrap the text and not pay attention to the newline thats in the string.


so the listbox will look like:

firstlineofstring
secondlineofstring


any help would be great.

TechnoGeek

  • Guest
Re: c# add new line to listbox
« Reply #1 on: March 25, 2013, 05:01:48 PM »
If I understand what you're trying to do, you'll have to split the downloaded string first before adding it.
Something like:
Code: [Select]
listbox1.Items.AddRange(downloadedString.Split('\n'));if AddRange is unavailable (I don't remember if you can use it on listbox item collections), then you can alternatively loop through the lines and add each one:
Code: [Select]
foreach (string line in downloadedString.Split('\n'))
   listbox1.Items.Add(line);

You may also need to tweak the arguments to the String.Split method above (see http://msdn.microsoft.com/en-us/library/y7h14879.aspx)

DaftHacker

    Topic Starter


    Rookie

  • I am Daft
    • Yes
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 8
Re: c# add new line to listbox
« Reply #2 on: March 25, 2013, 06:34:47 PM »
If I understand what you're trying to do, you'll have to split the downloaded string first before adding it.
Something like:
Code: [Select]
listbox1.Items.AddRange(downloadedString.Split('\n'));if AddRange is unavailable (I don't remember if you can use it on listbox item collections), then you can alternatively loop through the lines and add each one:
Code: [Select]
foreach (string line in downloadedString.Split('\n'))
   listbox1.Items.Add(line);

You may also need to tweak the arguments to the String.Split method above (see http://msdn.microsoft.com/en-us/library/y7h14879.aspx)
Second one worked, thanks a lot.