vb.netlistview

listview scrolling using topItem property


I need your help again.

I have a list of films grouped for the first letter of the title I'm writing a routine that when pressing two keys (PageDown and PageUp) is placed on the first title of the next letter or the previous one.

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

The first msgbox correctly displays the first record of each group. The msgbox indicated after the assignment of the item to the topItem property, always returns me the first record of the Listview does not make pagination work

Can anyone help me?

Thanks

Marcello


Solution

  • At a guess, I'd say that that TopItem property breaks when using groups. This code might be acceptable, which at least ensures that the first item of the next group is visible:

    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