嗨,我如何從列表框中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);
}
}
任何幫助將非常感謝。
只是一個每一次的問題! – Oscar
「爲什麼這段代碼停在catch(WebException x)」 - 因爲在try塊中拋出了一個異常(確切地說是一個'WebException')並且被捕獲塊捕獲? – Tim
@Tim是的,我知道這是因爲無效的URL,所以如何可以要求代碼繼續與下一個網址:) – terrala7