How to scroll through list items

I have a listview control in my WinForms application.

here when a separate button is clicked I change a couple of list items backcolor and reload the whole grid as there are certain changes in the database, so reloading from the database on every button click.

Now the problem is that after the grid reloads and then the added items are scrolled, so it is necessary to scroll through all the items and find them in a way that makes it difficult for the end user.

Is there a way to scroll the last added items or updated items in the listview automatically (I mean, programmatically so, it can be viewed directly by the user without manually scrolling).

+3


source to share


2 answers


listView1.EnsureVisible(X);

where X is the element index.

This snippet can be used to automatically scroll the ListView to a specific index in listView

.



Consider the code: with this, you can automatically scroll to the index 8

when the button is pressed

 private void button2_Click(object sender, EventArgs e)
 {
     listView1.EnsureVisible(8);
 }

      

+3


source


Before updating the list, save the current or selected item (depending on how the interaction code works) in a variable so you can restore the selected item later. For example:



Dim selectedObjectName = listview.SelectedItems(0).Name
...
' refresh your list
...
Dim vItem as ListViewItem
If listview.SelectedItem.ContainsKey(selectedObjectName) Then 
    vItem = listview.Items(selectedObjectName)
Else
    vItem = listview.Items(0)
End If
vItem.Selected = True
vItem.Focus

      

0


source







All Articles