Listview with groups&未排序的索引 - 获取下一个项目

时间:2018-06-07 09:46:05

标签: .net vb.net winforms listview

我有一个listview的一些组,包含未排序索引的项目 当递增所选项目索引+ 1时,这不一定是列表中的下一个项目。

如何找到列表中的下一个项目(在下一行)?

代码

Dim selected As Integer = LVMain.SelectedItems(0).Index  
LVMain.Items(selected + 1).Selected = True

(这是我的代码,它选择下一个项目BY INDEX,而不是按行)

1 个答案:

答案 0 :(得分:0)

来自vbforums.com的jmcilhinney通过以下代码/演示解决了这个问题:
SelectNextItem()包含解决方案。

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    With ListView1
        .View = View.Details
        .ShowGroups = True
        .HideSelection = False

        .Columns.Add("Description", "Description", 100)
        .Columns.Add("Index", "Index")

        Dim oddGroup = .Groups.Add("Odds", "Odds")
        Dim evenGroup = .Groups.Add("Evens", "Evens")

        AddItem("First", oddGroup)
        AddItem("Second", evenGroup)
        AddItem("Third", oddGroup)
        AddItem("Fourth", evenGroup)
        AddItem("Fifth", oddGroup)
        AddItem("Sixth", evenGroup)
        AddItem("Seventh", oddGroup)
        AddItem("Eighth", evenGroup)
        AddItem("Ninth", oddGroup)
        AddItem("Tenth", evenGroup)
    End With
End Sub

Private Sub AddItem(text As String, group As ListViewGroup)
    Dim item As New ListViewItem With {.Name = text, .Text = text, .Group = group}

    ListView1.Items.Add(item).SubItems.Add(item.Index.ToString())
End Sub

Private Sub ListView1_ColumnClick(sender As Object, e As ColumnClickEventArgs) Handles ListView1.ColumnClick
    ListView1.ListViewItemSorter = New ListViewItemSorter(e.Column)

    For Each item As ListViewItem In ListView1.Items
        item.SubItems(1).Text = item.Index.ToString()
    Next
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    SelectNextItem()
End Sub

Private Sub SelectNextItem()
    Dim selectedItem = ListView1.SelectedItems(0)
    Dim selectedGroup = selectedItem.Group

    'Get the item in the same group with the next highest index.
    Dim nextItem = selectedGroup.Items.
                                 Cast(Of ListViewItem)().
                                 OrderBy(Function(item) item.Index).
                                 FirstOrDefault(Function(item) item.Index > selectedItem.Index)

    If nextItem Is Nothing Then
        'The selected item is the last in its group so find the next group that contains items.
        Dim selectedGroupIndex = ListView1.Groups.IndexOf(selectedGroup)

        Do
            selectedGroupIndex += 1

            If selectedGroupIndex = ListView1.Groups.Count Then
                'Wrap to the first group after the last.
                selectedGroupIndex = 0
            End If

            selectedGroup = ListView1.Groups(selectedGroupIndex)
        Loop Until selectedGroup.Items.Count > 0

        'Get the first item in the next group.
        nextItem = selectedGroup.Items.
                                 Cast(Of ListViewItem)().
                                 OrderBy(Function(item) item.Index).
                                 First()
    End If

    'Change the selection.
    selectedItem.Selected = False
    nextItem.Selected = True
End Sub

End Class


Public Class ListViewItemSorter
Implements IComparer

Private ReadOnly columnIndex As Integer

Public Sub New(columnIndex As Integer)
    Me.columnIndex = columnIndex
End Sub

Public Function Compare(x As Object, y As Object) As Integer Implements IComparer.Compare
    Dim itemX = DirectCast(x, ListViewItem)
    Dim itemY = DirectCast(y, ListViewItem)

    Select Case columnIndex
        Case 0
            Return itemX.Text.CompareTo(itemY.Text)
        Case 1
            Return CInt(itemX.SubItems(1).Text).CompareTo(CInt(itemY.SubItems(1).Text))
    End Select

    Return 0
End Function

End Class
相关问题