Winforms: How can I programmatically display the last item in a C# listview when there are vertical scrollbars?

StackOverflow https://stackoverflow.com/questions/616491

Question

How can I programmatically display the last item in a C# listview when there are vertical scrollbars? I've studied every method associated with listviews and can't find anything.

Was it helpful?

Solution

It's not actually easy/possible to scroll the list view. You need to tell the item to make sure it's visible.

var items = listView.Items;
var last = items[items.Count-1];
last.EnsureVisible();

OTHER TIPS

this.listView1.Items[this.listView1.Items.Count - 1].EnsureVisible();  

ListViewItem.EnsureVisible()

WINFORMS:

Did you try setting the Selected value to TRUE in the last item in the Items collection of the ListView?

I think that doing this will focus on the last item... scrolling down if it is necesary. But I did't tryed myself.

EDIT: This will do the trick:

Me.ListView1.Items(Me.ListView1.Items.Count - 1).EnsureVisible()

WPF or WinForms?

In WPF, you get the ListViewItem and call BringIntoView on it.

This is a link to using a windows function to hide the horizontal and force vertical to be shown at all times:

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/4aa4dade-53a2-4e2e-a8b4-b4980da1f39c/

The following hack will both select and show the last ListView item.
Not sure why this works but it works.

listview.SelectedIndices.Clear();  
listview.FocusedItem = listview.Items[listview.Items.Count - 1];  
listview.FocusedItem.Selected = true;  
listview.BeginInvoke((MethodInvoker)delegate { 
    listview.FocusedItem.EnsureVisible(); 
});

Also, if you don't want a horizontal scroll bar to show, you need to resize ListView columns to fit the ListView's ClientArea width before calling BeginInvoke.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top