0

Hi how can i add items from 2 listBox to one listBox

ex: listBox1 contain Hello listBox2 contain World! So if button1 is clicked in listbox3 will display Hello World! side bye side but not in a new line like

Hello

World!

    private void button2_Click(object sender, EventArgs e)
    {
      listBox3.Items.Add(listBox1.Items + listBox2.Items);
    }

and 1 more how to make HttpWebRequest from 2 listBox.items

    private void button1_Click(object sender, EventArgs e)
    {
        WebRequest request = WebRequest.Create(listBox1.Items + listBox2.Items);
    }

ex: listBox1 contain http://test.com listBox2 contain /index.html So if button1 is clicked it will combine items from listBox1 and listBox2 into 1 item So it will become http://test.com/index.html and send the request to the website

and 1 more why is this code stop at catch (WebException x)

and why return false; not working when the button1_click is in void, i tried make the button to bool type but it will make the listBox1 error.

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < listBox1.Items.Count; i++)
            {
                // Create a request for the URL.        
                WebRequest request = WebRequest.Create(listBox1.Items[i].ToString());
                // If required by the server, set the credentials.
                request.Credentials = CredentialCache.DefaultCredentials;
                // Get the response.
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                // Display the status.
                // Get the stream containing content returned by the server.
                Stream dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream);
                // Read the content. 
                string responseFromServer = reader.ReadToEnd();
                // Display the content.
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    listBox2.Items.Add(listBox1.Items[i]);
                }
                // Cleanup the streams and the response.
                reader.Close();
                dataStream.Close();
                response.Close();
            }

            }
        catch (WebException x)
        {
            listBox2.Items.Add("Error! " + x.Message);
        } 
    }

Any help will be much appreciated thanks.

Trae Moore
  • 1,759
  • 3
  • 17
  • 32
terrala7
  • 65
  • 1
  • 8
  • 1
    Just one question each time! – Oscar Aug 24 '13 at 12:22
  • " why is this code stop at catch (WebException x)" - because an exception (a `WebException` to be exact) was thrown in the try block and caught in the catch block? – Tim Aug 24 '13 at 12:25
  • @Tim yes i know that's because the unactive url so how can ask the code continue with the next url :) – terrala7 Aug 24 '13 at 12:38

2 Answers2

0

For first part:

listbox3.items.add(listbox1.SelectedItem.ToString() + listbox2.SelectedItem.ToString());

For second part:

WebRequest request = WebRequest.Create(listBox1.SelectedItem.ToString() +   
listBox2.SelectedItem.ToString());

Final phase:

If exception occurs and different url is expected then do select different url entries    
from both listbox1 and listbox2 and click the button to check. Also keep correct 
entries in both the listboxes to avoid exception.
user1502952
  • 1,390
  • 4
  • 13
  • 27
  • Ops what if i have multiple list from the listbox how to make them auto without needed to select them? – terrala7 Aug 24 '13 at 15:56
0
  1. Get selected item from a ListBox: ListBox1.SectedItem
  2. Concatenate strings: string1 + string2
  3. Add item to ListBox: ListBox1.Items.Add("item_name_or_value")
rens
  • 89
  • 2
  • 9