Winforms:当有垂直滚动条时,如何以编程方式显示 C# 列表视图中的最后一项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/616491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:22:50  来源:igfitidea点击:

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

c#winformslistviewscrollbar

提问by Auburnate

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.

当有垂直滚动条时,如何以编程方式显示 C# 列表视图中的最后一项?我研究了与列表视图相关的所有方法,但找不到任何东西。

采纳答案by JaredPar

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();

回答by Daniel Earwicker

WPF or WinForms?

WPF 还是 WinForms?

In WPF, you get the ListViewItemand call BringIntoViewon it.

在 WPF 中,您可以获得ListViewItem并调用BringIntoView它。

回答by Romias

WINFORMS:

WINFORMS:

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

您是否尝试在 ListView 的 Items 集合的最后一项中将 Selected 值设置为 TRUE?

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()

回答by Michael Damatov

ListViewItem.EnsureVisible()

ListViewItem.EnsureVisible()

回答by BFree

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

回答by JaredBroad

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

这是使用 windows 函数隐藏水平并始终强制显示垂直的链接:

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

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

回答by bricklayer137

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

以下 hack 将选择并显示最后一个 ListView 项目。
不知道为什么这有效,但它有效。

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 ClientAreawidth before calling BeginInvoke.

此外,如果您不想显示水平滚动条,则需要ClientArea在调用BeginInvoke.