使用topItem属性滚动列表视图

时间:2018-08-16 05:51:23

标签: vb.net listview

我再次需要您的帮助。

我有一个电影列表,按照标题的首字母分组 我正在编写一个例程,当按下两个键(PageDown和PageUp)时,将其放在下一个字母或上一个字母的第一个标题上。

Private Sub frmMovieDatabase_KeyDown(sender As Object, e As KeyEventArgs) Handles lvFilmDetail.KeyDown
    If Not lvFilmDetail.ShowGroups Then
        Exit Sub
    End If

    Dim ctr_max_groups As Integer = lvFilmDetail.Groups.Count - 1
    Dim ctr_curr_group As Integer = 0

    If e.KeyCode = Keys.PageDown Or
        e.KeyCode = Keys.PageUp Then

        Select Case e.KeyCode
            Case Keys.PageDown
                ctr_curr_group = lvFilmDetail.Groups.IndexOf(currentGroup) + 1
                If ctr_curr_group > ctr_max_groups Then
                    ctr_curr_group = 0
                End If
                currentGroup = lvFilmDetail.Groups.Item(ctr_curr_group)

            Case Keys.PageUp
                ctr_curr_group = lvFilmDetail.Groups.IndexOf(currentGroup) - 1
                If ctr_curr_group < 0 Then
                    ctr_curr_group = ctr_max_groups
                End If

                currentGroup = lvFilmDetail.Groups.Item(ctr_curr_group)
        End Select

        MsgBox(currentGroup.Items(0).Text)
        lvFilmDetail.TopItem = currentGroup.Items(0)
        MsgBox(lvFilmDetail.TopItem.Text)

        e.SuppressKeyPress = True

    End If


End Sub

第一个msgbox正确显示每个组的第一个记录。在将项目分配给topItem属性后指示的msgbox总是返回我Listview的第一条记录无法使分页工作

有人可以帮助我吗?

谢谢

马塞洛(Marcello)

1 个答案:

答案 0 :(得分:0)

猜测,我想说TopItem属性在使用组时会中断。这段代码可能是可以接受的,至少可以确保下一组的第一项可见:

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
    If e.KeyCode <> Keys.PageUp AndAlso e.KeyCode <> Keys.PageDown Then Return

    Dim groups = ListView1.Groups
    Dim groupCount = groups.Count
    Dim group = ListView1.FocusedItem.Group
    Dim groupIndex = groups.IndexOf(group)

    Select Case e.KeyCode
        Case Keys.PageUp
            groupIndex -= 1

            If groupIndex < 0 Then
                groupIndex = groupCount - 1
            End If
        Case Keys.PageDown
            groupIndex += 1

            If groupIndex = groupCount Then
                groupIndex = 0
            End If
    End Select

    group = groups(groupIndex)

    Dim item = group.Items(0)

    ListView1.FocusedItem = item
    ListView1.EnsureVisible(item.Index)

    e.SuppressKeyPress = True
End Sub