VB Listview第二列从第一列的最后一行开始

时间:2015-06-12 20:40:07

标签: vb.net visual-studio listview

我想在listview第1列和第2列输入信息。我在第1列和第2列中获得了我想要的信息,但第2列中的信息将从第1列的最后一行开始。我希望第2列也是从顶部开始。希望你明白我的意思,这里是截图here a screenshot

Dim r As New System.Text.RegularExpressions.Regex("""ID"".*""")
Dim matches As MatchCollection = r.Matches(sr.ReadToEnd)

   For Each itemcode As Match In matches

            Dim Col1 As String = itemcode.Value.Split(":").GetValue(1)
            Dim lvi As New ListViewItem
            lvi.Text = Col1
            ListView1.Items.Add(lvi)
        Next     

Dim r2 As New System.Text.RegularExpressions.Regex("""ID"".*""")
Dim matches2 As MatchCollection = r2.Matches(sr.ReadToEnd)

   For Each itemcode2 As Match In matches2

            Dim Col2 As String = itemcode2.Value.Split(":").GetValue(1)
            Dim lvi As New ListViewItem
            lvi.SubItems.Add(Col2)
            ListView1.Items.Add(lvi)
       Next

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:0)

假设在第二种方法中,您正在旋转与第一种方法相同的列表(或具有相同计数的列表),以下代码将起作用。这是建议使用Plutonix并在已创建的ListItems中添加子项。

    Dim intCursor As Integer = 0
    Do While intCursor < lstYourList.Count

        'Assumes your list is called lstYourList
        Dim strSubItemToAdd As String = lstYourList(intCursor).Value.Split(":").GetValue(1)

        ListView1.Items(intCursor).SubItems.Add(strSubItemToAdd)

        intCursor += 1

    Loop

如果您坚持保持正则表达式匹配的每次迭代(根据原始答案的评论),那么它将更接近于此:

Dim intCursor As Integer

For Each itemcode2 As Match In YourRegex.Matches(strSource)

    ListView1.Items(intCursor).SubItems.Add(itemcode2.Value.Split(":").GetValue(1))

    intCursor += 1

Next

好的,让我根据你的评论多加一点。我更改了上面的代码,以便将第二个Regex Matches列表添加到第二列。您会注意到,如果我们已超过ListView中ListViewItem的总数,我只会添加一个新的ListViewItem。否则,我只是将它们添加为SubItems。

Dim r As New System.Text.RegularExpressions.Regex("""ID"".*""")
Dim matches As MatchCollection = r.Matches(sr.ReadToEnd)

For Each itemcode As Match In matches

    Dim Col1 As String = itemcode.Value.Split(":").GetValue(1)
    Dim lvi As New ListViewItem
    lvi.Text = Col1
    ListView1.Items.Add(lvi)

Next     

Dim r2 As New System.Text.RegularExpressions.Regex("""ID"".*""")
Dim matches2 As MatchCollection = r2.Matches(sr.ReadToEnd)

Dim intCursor As Integer

For Each itemcode2 As Match In matches2

    Dim Col2 As String = itemcode2.Value.Split(":").GetValue(1)

    If intCursor = ListView1.Items.Count Then

        ListView1.Items(intCursor).SubItems.Add(Col2)

    Else

        Dim lvi2 As New ListViewItem
        lvi2.SubItems.Add(Col2)

        ListView1.Items.Add(lvi2)       

    End If

    intCursor += 1

Next