【问题标题】:Textbox text to listbox items vb.net文本框文本到列表框项目 vb.net
【发布时间】:2011-10-27 18:17:01
【问题描述】:

好的,所以我有一个文本框,它可以从网站获取项目并粘贴到其中,然后列表框会添加项目,但我希望文本框中的每一行都是一个新项目,而不是将其全部添加为一个项目

这是我的代码

    '  Procedure:
    Dim Str As System.IO.Stream
    Dim srRead As System.IO.StreamReader
    Try
        ' make a Web request
        Dim req As System.Net.WebRequest = System.Net.WebRequest.Create("http://76.31.248.130/videos.txt")
        Dim resp As System.Net.WebResponse = req.GetResponse
        Str = resp.GetResponseStream
        srRead = New System.IO.StreamReader(Str)
        ' read all the text 
        TextBox2.Text = srRead.ReadToEnd
    Catch ex As Exception
        TextBox2.Text = "Unable to download content"
    Finally
        '  Close Stream and StreamReader when done
        srRead.Close()
        Str.Close()
    End Try
    ' Assign string to reference.
    Dim value1 As String = TextBox2.Text


    ' Replace word with another word.
    Dim value2 As String = value1.Replace("<br>", vbNewLine)
    TextBox2.Text = value2
    ListBox1.Items.Add(TextBox2.Text)

【问题讨论】:

    标签: vb.net listbox


    【解决方案1】:

    试试

    ListBox1.Items.AddRange(TextBox2.Lines)
    

    而不是

    ListBox1.Items.Add(TextBox2.Text)
    

    【讨论】:

      【解决方案2】:

      尝试使用srRead.readline 而不是srRead.readtoend

      Dim a As String
      Try
              Do
                  a = srRead.ReadLine
                  If a <> Nothing Then
                      ListBox1.Items.Add(a)
                  End If
              Loop Until a Is Nothing
          Catch
          End Try
      

      对不起,如果我没能理解你的问题

      【讨论】:

        【解决方案3】:

        就这么简单 ListBox1.Items.AddRange(TextBox1.Text.Split(vbNewLine)) 或 ListBox1.Items.AddRange(TextBox1.Text.Split(vbcrlf)

        【讨论】:

          【解决方案4】:

          你的问题很难理解。我认为答案是将文本框的文本拆分为一个数组,其中每个项目都是一行,然后将每个项目添加到列表框中。

          你可能想要:

          ListBox1.Items.AddRange(TextBox2.Text.Split(vbNewLine))
          

          【讨论】:

            猜你喜欢
            • 2014-07-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-01-11
            相关资源
            最近更新 更多