如何使列表视图在中心显示特定项目? [英] How to make the listview to show a specific item in the center?
问题描述
是否存在一种通用方法将特定列表项(例如1000中的第500个)放在其中心位置?现在,我正在使用此代码:
Is there a universal method of placing a specific item (e.g. 500th from 1000) of the listview right in its center? Now I am using this code:
lvData.Items[iIndex].MakeVisible(False);
这很简单,但有一个缺陷-大多数必需项出现在列表视图的顶部或底部.是的,我知道,手动滚动它没什么大不了,但是我使用它的方式(在图表上选择一个点并在列表视图中查看附近点的值)使此行为有些不适.
It's simple but has one flaw - mostly the required item appears at the top or at the bottom of the listview. Yes I know, it's not a big deal to scroll it manually but the way I use it (selecting a point on the graph and viewing the values of the nearby points in the listview) makes this behavior a little uncomfortable.
推荐答案
您可以使用项目的DisplayRect
来确定当前项目的位置.
鉴于ListView1
是列表视图,li
是列表项,而R
是TRect
变量
You can use DisplayRect
of an item to determine where it currently lives.
Given ListView1
is the listview, li
is the list item and R
is a TRect
variable
R := li.DisplayRect(drBounds);
ListView1.Scroll(0, R.Top - ListView1.ClientHeight div 2);
如果有足够的项目,将在中心滚动该项目.
will scroll the item in the center, provided there are enough items.
这篇关于如何使列表视图在中心显示特定项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!