Solved

C# Add to listbox

Posted on 2011-02-13
9
439 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
A short film showing how OnPage and Connectwise integration works.

919 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

21 Experts available now in Live!

Get 1:1 Help Now