How to listview scrolling using topItem property?Ask Questions

 Posted on 08/16/2018

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

        lvFilmDetail.TopItem = currentGroup.Items(0)

        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?

Share On: facebook gplus twitter
Asked by hardik chaudhary on 08/16/2018 Score: 553 points
Add Comment:


1 Answers

1 Corrected Answers
Aproved Answers
Answered by on 8/17/2018 10:19:21 AM Score: 89 points

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

    e.SuppressKeyPress = True
End Sub


Add Comment:

Post Your Answers

Existing Members

Sign in to your account
Email Address
New Member?
Sign up and complete profile
Full Name
Email Address
I have read and agree to the Terms of Service and Privacy Policy
Please subscribe me to the StoodQ newsletters
Guideline to answer a question:

Useful tips to submit your answer
Please read below guidelines before you submit your answer for question.

  • Read and understand question for which you are submitting your answer.
  • Try to avoid grammatical and spell mistake while answering.
  • Do not post any irrelevant information in your answer.
  • Explain your answer with example or any reference link to help who posted question.
  • If you find irrelevant question, please report it to support. Click here to contact support.
  • You agree to the privacy policy and terms of use to submit any contents.

Note: StoodQ is online developers community which helps developer for their difficulty, lets help them with your value contribution.