2013-08-24 168 views
0

嗨,我如何從列表框中2項添加到列表框一個從2列表框項目添加到列表框一個

例如: listBox1中含有你好 listBox2包含世界! 所以如果在listbox3中點擊button1將顯示Hello World!旁邊但不在新行像

你好

世界!

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

和1更如何使HttpWebRequest的2個listBox.items

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

例如: listBox1中含有http://test.com listBox2包含/index.html 因此,如果按鈕1被點擊它將從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); 
     } 
    } 

任何幫助將非常感謝。

+1

只是一個每一次的問題! – Oscar

+0

「爲什麼這段代碼停在catch(WebException x)」 - 因爲在try塊中拋出了一個異常(確切地說是一個'WebException')並且被捕獲塊捕獲? – Tim

+0

@Tim是的,我知道這是因爲無效的URL,所以如何可以要求代碼繼續與下一個網址:) – terrala7

回答

0

對於第一部分:

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. 
+0

OPS如果我有多個列表中的列表框如何讓他們自動而無需選擇它們? – terrala7

0
  1. 獲得從列表框選擇的項目:ListBox1.SectedItem
  2. 連接字符串:字符串1 +字符串2
  3. 添加項目列表框:ListBox1.Items.Add( 「item_name_or_value」)
+0

感謝如何讓他們自動而無需選擇它們? – terrala7

相關問題