c# listbox 获取显示的索引范围 [英] c# listbox get displayed range of indices
问题描述
我有一个列表框,我想显示一个标签:
i have a listbox and i would want to display a label displaying:
滚动查看 ZZZ 的 XXX 到 XYY 项.
scrolling through items XXX to XYY of ZZZ.
我该怎么做,因为使用 SelectedIndex 没有用,因为即使没有选择任何内容,我也希望标签更新.(也滚动,它不会选择一个项目).
how do I do this because using SelectedIndex will not be useful, as i would like the label to update even when nothing is selected. (scrolling too, it does not select an item).
更新:例如,我的列表框中有 200 个项目.由于列表框的高度,我在任何时候都只能显示 10 个项目.所以标签应该是:
update: for example I have 200 items in my listbox. at any one time i can only display only 10 items because of my listbox's height. so the label should read:
显示项目 1 到 10,共 200 个
displaying items 1 to 10 of 200
或
显示第 5 到 15 个项目,共 200 个
displaying items 5 to 15 of 200
但是我必须考虑到可能没有选择任何索引,因为我可以简单地滚动而不选择任何内容.
however i must take into account that there may not be any indices selected because i can simply scroll and not select anything.
推荐答案
您可以使用 listbox.TopIndex
获取顶部索引值,使用 listbox.Items.Count
获取计数> 但我看不出有什么方法可以从 listbox.GetItemHeight()
和 listbox.ClientSize.Height
的结果计算它的底部项目:
You can get the top index value using listbox.TopIndex
and the count using listbox.Items.Count
but I can't see any way to get the bottom item withotu calculating it from the result of listbox.GetItemHeight()
and listbox.ClientSize.Height
:
int visibleCount = listBox1.ClientSize.Height / listBox1.ItemHeight;
this.Text = string.Format("{0:d} to {1:d} of {2:d}", listBox1.TopIndex + 1, listBox1.TopIndex + visibleCount, listBox1.Items.Count);
这可以在计时器上完成,因为我看不到滚动事件.
This can be done on a timer as I see no scroll event.
这篇关于c# listbox 获取显示的索引范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!