We help IT Professionals succeed at work.

Programmatically iterating back and forth through a listview

JRossi1
JRossi1 asked
on
Medium Priority
335 Views
Last Modified: 2012-05-05
Greetings Experts!

I have two buttons:  'Next' and 'Previous'.  I have a listview that contains screen names.  I need the buttons to programmatically select the screen names in the listview starting at the first one then when the 'Next' button is clicked, the subsequent screen is selected in the listview.  When the 'Previous' button is clicked, the prior screen name is selected.

The multiselect property of the ListView is false so that when one screen name is selected, the prior screen name is de-selected.
Comment
Watch Question

CERTIFIED EXPERT
Commented:
With lstScreenNames as the listview, btnNext and btnPrevious as your buttons:

    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        Dim intItem As Integer
        intItem = lstScreenNames.SelectedIndices.Item(0)
        If intItem > 0 Then
            lstScreenNames.Items(intItem - 1).Selected = True
        End If
        lstScreenNames.Select()
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        Dim intItem As Integer
        intItem = lstScreenNames.SelectedIndices.Item(0)
        If intItem < lstScreenNames.Items.Count - 1 Then
            lstScreenNames.Items(intItem + 1).Selected = True
        End If
        lstScreenNames.Select()
    End Sub

For lstScreenNames, you may want to set the HideSelection property to False so the selection is visible if another control is selected.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Perfect.  Thanks!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.