【问题标题】:Add items from 2 listBox to one listBox将 2 个 listBox 中的项目添加到一个 listBox
【发布时间】:2013-08-24 12:18:34
【问题描述】:

您好,我如何将 2 个列表框的项目添加到一个列表框

例如: listBox1 包含你好 listBox2 包含 World! 因此,如果在 listbox3 中单击 button1 将显示 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.com listBox2 包含/index.html 因此,如果单击 button1,它将将 listBox1 和 listBox2 中的项目合并为 1 个项目 所以它会变成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);
        } 
    }

任何帮助将不胜感激。

【问题讨论】:

  • 每次只有一个问题!
  • "为什么这段代码在 catch (WebException x) 处停止" - 因为在 try 块中抛出了异常(准确地说是 WebException)并在 catch 块中被捕获?
  • @Tim 是的,我知道那是因为不活动的 url,所以如何让代码继续下一个 url :)

标签: c# .net webrequest


【解决方案1】:

第一部分:

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.

【讨论】:

  • 操作如果我从列表框中有多个列表如何使它们自动而不需要选择它们?
【解决方案2】:
  1. 从 ListBox 中获取选定项:ListBox1.SectedItem
  2. 连接字符串:string1 + string2
  3. 将项目添加到列表框:ListBox1.Items.Add("item_name_or_value")

【讨论】:

  • 感谢如何使它们自动而不需要选择它们?
猜你喜欢
  • 2013-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多