简体   繁体   中英

Add items from 2 listBox to one listBox

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.

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.
  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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM