如何将文本文件项加载到ListView VB .Net

时间:2014-08-12 12:20:31

标签: vb.net listview

我创建了包含5列的ListView1。 每列都有不同的名称,我需要加载'从我的PC通过打开对话框功能并在ListView1中添加文本文件。 问题是,一旦我继续并在ListView1中加载txt文件,它就不会重新识别特定的值'喜欢"代理" - >到'代理列' " IP" - >到了' IP'专栏等.... 我正在从文本文件中添加formate,如下所示:192.169.0.1:8080这就是我需要通过标签名称在colmuns中仅添加这2个值。其他列名称是因为当我检查代理时,它将显示(速度,协议和类型) 所以这里是我的代码:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ListView1.View = View.Details
    ListView1.GridLines = True
    ListView1.FullRowSelect = True
    ListView1.HideSelection = False
    ListView1.MultiSelect = False
    ListView1.Columns.Add("IP", 90, HorizontalAlignment.Left)
    ListView1.Columns.Add("Port", 40, HorizontalAlignment.Left)
    ListView1.Columns.Add("Protocol", 80, HorizontalAlignment.Left)
    ListView1.Columns.Add("Type", 60, HorizontalAlignment.Left)
    ListView1.Columns.Add("Speed", 50, HorizontalAlignment.Left)
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    OpenFileDialog1.ShowDialog()
    Dim filepath As String = OpenFileDialog1.FileName
    Dim inputstream As New IO.StreamReader(filepath)
    Dim newstr(2) As String
    Dim IP As String
    Dim Port As String
    Dim Protocol As String
    Dim Type As String
    Dim Speed As String

    'Read while there is mor characters to read
    Do While inputstream.Peek <> -1
        'Split each line containing Account|Password into the array
        newstr = inputstream.ReadLine().Split("|")
        'Assigm the values to the variables
        IP = newstr(0)
        Port = newstr(1)
        Protocol = newstr(2)
        Type = newstr(3)
        Speed = newstr(4)

        'Add them to the list
        Me.ListView1.Items.Add(IP)
        Me.ListView1.Items.Item(ListView1.Items.Count - 1).SubItems.Add(Port)
        Me.ListView1.Items.Item(ListView1.Items.Count - 1).SubItems.Add(Protocol)
        Me.ListView1.Items.Item(ListView1.Items.Count - 1).SubItems.Add(Type)
        Me.ListView1.Items.Item(ListView1.Items.Count - 1).SubItems.Add(Speed)
    Loop

    inputstream.Close()
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    For Each Item As ListViewItem In ListView1.Items
        ListView1.Items.Remove(Item)
    Next
End Sub

所以你能否告诉我如何添加代码打开对话框识别我的前2个属性值并将它们正确添加到列中? THX

1 个答案:

答案 0 :(得分:0)

/)我从代码中删除了这个:

        Protocol = newstr(2)
        Type = newstr(3)
        Speed = newstr(4)

以及此之后:

            Me.ListView1.Items.Item(ListView1.Items.Count - 1).SubItems.Add(Protocol)
            Me.ListView1.Items.Item(ListView1.Items.Count - 1).SubItems.Add(Type)
            Me.ListView1.Items.Item(ListView1.Items.Count - 1).SubItems.Add(Speed)

它工作!!!! 因为我只需要插入IP:PORT格式和其他不需要的信息,因为只有在listview1中验证或抓取时才会这样,而不是从PC上传:D PS:顺便说一句GRX,因为他帮我和我改变了“|”这个以“:”因为我的代理人是半colomn分开的,而且他是VINCENT,因为他帮我和我改变了“Dim newstr()As String”所以现在一切都好了:-) youpeee:p ..也很抱歉,我没有看到任何选项,我怎么能标记这2名成员的答案,所以我自己回答,我在那里提到你了,谢谢!