Solved

C# Add to listbox

Posted on 2011-02-13
9
465 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 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