C# Add to listbox

the code below it add only one line to list box how can I correct to make it insert for all text
private void button1_Click(object sender, EventArgs e)
        {
            string url = "http://www.presscouncil.or.th/th2/index.php?option=com_content&view=article&id=230&Itemid=100084";
            string result = null;

            try
            {
                WebClient client = new WebClient();
                result = client.DownloadString(url);
            
                listBox1.Items.Add(result);
            }
            catch (Exception ex)
            {
                // handle error
                MessageBox.Show(ex.Message);
            }

Open in new window

teeraAsked:
Who is Participating?
 
starlite551Connect With a Mentor Commented:
Sorry the correct code is as follows :
private void button1_Click(object sender, EventArgs e)
        {
            string url = "http://www.presscouncil.or.th/th2/index.php?option=com_content&view=article&id=230&Itemid=100084";
            string result = null;
            try
            {
                WebClient client = new WebClient();
                result = client.DownloadString(url);
                string[] lines = result.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string line in lines)
                {
                    listBox1.Items.Add(line);
                }
            }
            catch (Exception ex)
            {
                // handle error    
                MessageBox.Show(ex.Message);
            }
        }

Open in new window

0
 
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
That code asks for ONLY one line to be added to the list box.

If you want multiple lines then you need to call Items.Add once for each string you want adding.
0
 
mkobrinCommented:
From what I can make out your result data is stored in a single string. you will need to split the string into an array, or loop your webclient call to fetch more results.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
Hi, use this code

private void textBox_Leave(object sender, System.EventArgs e)
{
    listBox1.Items.Add(((TextBox)sender).Text);
}
0
 
starlite551Commented:
try using this code :
private void button1_Click(object sender, EventArgs e)  
{  
      string url = "http://www.presscouncil.or.th/th2/index.php?option=com_content&view=article&id=230&Itemid=100084";  
      string result = null;  
      try  
      {  
           WebClient client = new WebClient();  
           result = client.DownloadString(url); 
           string[] lines = result.Split(new string[]{"\n"},StringSplitOptions.RemoveEmptyEntires);
           foreach(string line in lines)
           { 
                 listBox1.Items.Add(line);  
           }
      }  
      catch (Exception ex)  
      {  
          // handle error  
          MessageBox.Show(ex.Message);  
      }
}

Open in new window



Above code will add all the content line by line.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
I can understand the comment accepted.
The assist of mkobrin says in different words what I said, so I am surprised that gets an assist but my comment doesn't.

Of what use is the comment by samirbhogayta in resolving your problem?
0
 
starlite551Commented:
I agree with Andy.. :)
0
All Courses

From novice to tech pro — start learning today.