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

8

12

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.

Kavanagh answered 5/3, 2009 at 20:31 Comment(0)
B
13

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();
Bruxelles answered 5/3, 2009 at 20:37 Comment(2)
This code works, but a horizontal bar shows up and conceals it on my CE device (due to the limited size). Any way to diable horizontal scroll bar?Kavanagh
You need to take into account the vertical scrollbar width. Your columns should't use all the width of your ListView... save a little bit for the vertical scroll bar.Alansen
C
6
this.listView1.Items[this.listView1.Items.Count - 1].EnsureVisible();  
Continental answered 5/3, 2009 at 20:37 Comment(0)
A
1

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()
Alansen answered 5/3, 2009 at 20:35 Comment(1)
It set the focus but did not auto scroll. Any other ideas?Kavanagh
T
1

ListViewItem.EnsureVisible()

Tetravalent answered 5/3, 2009 at 20:36 Comment(0)
L
0

WPF or WinForms?

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

Latoya answered 5/3, 2009 at 20:34 Comment(0)
L
0

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/

Lareelareena answered 15/9, 2009 at 21:53 Comment(0)
U
0

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.

Undoing answered 13/3, 2011 at 7:41 Comment(0)
H
0

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}");
Haughay answered 6/5, 2021 at 22:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.