【发布时间】:2017-12-04 17:57:51
【问题描述】:
我目前支持使用 Visual Studio 2008 的 C# 桌面应用程序(计划很快升级)。
我有一个包含 3000 多个项目的“列表视图”。由于项目数量众多,我创建了一个“TextBox”,以便用户开始输入并将匹配的“ListView”项目滚动到“ListView”的顶部。
我已经看到很多关于如何实现这一点的帖子,问题,据称满足此要求的问题并不能始终如一地工作。对于“ListView”,没有“Groups”和属性“ShowGroups”设置为“false”。我读到“ShowGroups”属性的“true”可能会导致问题。
我发现应该完成任务的代码如下。
private void txtSearchCompany_TextChanged(object sender, EventArgs e)
{
// Call FindItemWithText with the contents of the textbox.
ListViewItem foundItem =
lvCompany.FindItemWithText(txtSearchCompany.Text, true, 0, true);
if (foundItem != null)
{
lvCompany.TopItem = foundItem;
}
}
我已经单步执行了代码,但赋值语句只是偶尔起作用,这让我感到困惑。
例如在“TextBox”中输入“02”,这里是赋值语句的断点和执行赋值语句之前的值。
lvCompany.topItem {Text = "00000"}
foundItem {Text = "02057"}
现在是赋值语句之后的值。
lvCompany.topItem {Text = "01563"}
foundItem {Text = "02057"}
为什么“lvCompany.TopItem”会被设置为“01563”????
这是在“TextBox”中输入“02”后的“ListView”。 “ListView”中的顶部项目中没有“02”。 “TopItem”是“01563”。
【问题讨论】:
-
02057 下是否有足够的行可以将 02057 放在可视列表视图的顶部?如果没有,02057 只有在它下面有足够的行来将列表视图填充到可见视图的底部时才会向上移动。
-
是的,“02057”是总共 10,767 行中的第 311 行(比我最初想象的要多)。 “ListView”具有固定大小,最多显示 20 行。因此,我对为什么“02057”不滚动到顶部而“01563”滚动到顶部感到困惑。
标签: c# listview visual-studio-2008 properties