Click here to Skip to main content
15,881,693 members
Please Sign up or sign in to vote.
5.00/5 (4 votes)
See more:
Hi,
I need a way to automatically scroll to a selected item in a list view. For example: There are 10 items in the list view and only the first 5 are visible. A function selects the 9th item and the list view should automatically scroll to the 9th item. I tried using the following code.
listView1.Items(8).Selected = True
listView1.Select()
listView1.SelectedItems(8).EnsureVisible

But it doesn't work. Please help

I've went through the code again and found out something new. It seems like the list view is actually selecting the item but it doesn't highlight the selected item in the list view and it also does not scroll to the selected item.

I tried refresh and update methods in the list view but it does not seem to be working. Please help
Posted
Updated 29-Mar-22 17:45pm
v4
Comments
Sergey Alexandrovich Kryukov 9-May-11 2:58am    
Tag it! Forms, WPF, what?!
--SA
obhasha07 9-May-11 3:30am    
sorry mate the updated answer doesn't seem to be working. I don't know what's going on. The code I have given in my question worked when I run it the first time. But after a while it doesn't work. I've debugged the application and it even goes through every line. Please help!!!
Tarun.K.S 9-May-11 6:36am    
All I can say is weird!

In WPF, you can use ScrollIntoView.

listView1.ScrollIntoView(listView1.Items[8]);


UPDATE: After your updated question, since its a WinForms application,
I tried in this way:

listView1.EnsureVisible(8);

Worked for me. Did it work for you?
 
Share this answer
 
v2
Comments
obhasha07 9-May-11 3:03am    
It's a windows forms application. So that doesn't help. But thanks anyway.
Tarun.K.S 9-May-11 3:07am    
I have updated the answer.
majid30000 30-Oct-19 7:36am    
yes, it works for me.
C#
ListView_ProgressStatus.Items[ListView_ProgressStatus.Items.Count - 1].EnsureVisible();
 
Share this answer
 
Comments
Sunilxcl 19-Mar-18 1:55am    
It works. Thanks
Here is another solution:

VB
ListView1.TopItem = ListView1.Items(8)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900