Textbox text to listbox items vb.net

守給你的承諾、 提交于 2019-12-25 19:33:29

问题


Ok so i have a textbox that gets items from a website and pastes in that then the list box adds the items but i want each line in textbox to be a new item instead it just adds it all as one

heres my code

    '  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)

回答1:


Your question is very hard to understand. I think the answer is to split the textbox's text into an array where each item is a single line and then add each of these to the listbox.

You probably want:

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



回答2:


ITS AS SIMPLE AS ListBox1.Items.AddRange(TextBox1.Text.Split(vbNewLine)) oR ListBox1.Items.AddRange(TextBox1.Text.Split(vbcrlf)




回答3:


try using srRead.readline instead of 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

Sorry if i failed to understand your question



来源:https://stackoverflow.com/questions/7073532/textbox-text-to-listbox-items-vb-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!