Scrolling For ListView Selected Item

If you selected an item in ListView control within your code, Not always the item will be visible to the user.
This sample code will assure that the selected item will be visible.

Preparations

Add ListView control to your form (Named ListView1)
Set the ListView View property to 3 - lvwReport
Right click on the ListView Control, choose Properties from the menu, select the 'Column Headers' Tab, Press 'Insert Column' button, type in the 'Text' Text Box "MyHeader", and press the OK button.

Form Code

Private Sub Form_Load()
   Dim x As Integer
'the 4 lines below add 20 items to your ListView:
'MyItem1 named 'Item 1', MyItem2 named 'Item 2' and so on.

  With ListView1
      For x = 1 To 20
         .ListItems.Add Key:="Item " & x, Text:="MyItem" & x
       Next x
'Replace the "Item 20" below with the Item name that you want to select
      
.SelectedItem = .ListItems("Item 20")
       .SelectedItem.EnsureVisible
   End With
End Sub

Go Back