Solved

C# Add to listbox

Posted on 2011-02-13
9
426 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:teera
9 Comments
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 200 total points
ID: 34885853
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
 
LVL 7

Expert Comment

by:mkobrin
ID: 34885867
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
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 34886175
Hi, use this code

private void textBox_Leave(object sender, System.EventArgs e)
{
    listBox1.Items.Add(((TextBox)sender).Text);
}
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 12

Expert Comment

by:starlite551
ID: 34886505
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
 
LVL 12

Accepted Solution

by:
starlite551 earned 300 total points
ID: 34886608
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 34894777
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
 
LVL 12

Expert Comment

by:starlite551
ID: 34899998
I agree with Andy.. :)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now