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.
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();
this.listView1.Items[this.listView1.Items.Count - 1].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
.
I have a custom control that inherits the ListView but since it does not expose the inner ListView I had no way to utilize the above mentioned items...EnsureVisible()
solution.
I solved as sort of work around by sending the Ctrl+End keys directly to that control to manually fix the scroll to bottom:
logMsgList.Focus();
SendKeys.Send("^{END}");
© 2022 - 2024 — McMap. All rights reserved.