将两个列表框中的项目添加到一个列表框

本文关键字:列表 添加 项目 一个 两个 | 更新日期: 2023-09-27 18:28:48

嗨,我如何将2个listBox中的项目添加到一个listBox 中

例如:listBox1包含HellolistBox2包含世界!因此,如果在列表框3中单击按钮1,将显示Hello World!边对边,但不是像那样的新线路

你好

世界!

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

以及1如何从2 listBox.items 中制作HttpWebRequest

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

例如:listBox1包含http://test.comlistBox2包含/index.html因此,如果单击按钮1,它将把listBox1和listBox2中的项目组合成一个项目因此它将变成http://test.com/index.html并将请求发送到网站

还有1个为什么这个代码在catch处停止(WebException x)

以及为什么返回false;当button1_click处于无效状态时无法工作,我尝试将按钮设置为bool类型,但这会导致listBox1错误。

    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);
        } 
    }

任何帮助都将不胜感激,谢谢。

将两个列表框中的项目添加到一个列表框

对于第一部分:

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

第二部分:

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

最后阶段:

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. 从列表框中获取所选项目:ListBox1.SectedItem
  2. 连接字符串:字符串1+字符串2
  3. 将项添加到列表框:ListBox1.Items.Add("item_name_or_value")