0

こんにちは、2 つの listBox から 1 つの listBox に項目を追加するにはどうすればよいですか

例: listBox1 には Hello listBox2 には World! が含まれます。したがって、listbox3 で button1 をクリックすると、Hello World! が表示されます。並んでいるが、新しい行のようなものではない

こんにちは

世界!

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

2つのlistBox.itemsからHttpWebRequestを作成する方法がもう1つあります

    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、Web サイトにリクエストが送信されます。

そしてもう1つ、このコードがcatch (WebException x)で停止するのはなぜですか

なぜ false を返すのか; button1_click が void のときに機能しないので、ボタンを 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);
        } 
    }

どんな助けでも大歓迎ですありがとう。

4

2 に答える 2

0

最初の部分:

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

2 番目の部分の場合:

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.
于 2013-08-24T14:48:40.463 に答える
0
  1. ListBox から選択した項目を取得する: ListBox1.SectedItem
  2. 文字列の連結: string1 + string2
  3. ListBox に項目を追加: ListBox1.Items.Add("item_name_or_value")
于 2013-08-24T15:37:54.047 に答える