?
Solved

C# Add to listbox

Posted on 2011-02-13
9
Medium Priority
?
472 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 800 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 1200 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

719 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