我想在 ListView (WinForms) 中隐藏一些基于文本过滤器的项目。
基本上,列表视图从文本文件加载项目,我不希望在用户搜索列表时读取和/或写入它。搜索是在组合框的 KeyDown 事件中完成的,但 ListViewItem 没有“Visible”属性。
有没有简单的方法可以做到这一点,而无需重新读取文件?
(因为它是一个 XML 文件,甚至可能包含数千个项目,所以很难有效地搜索,甚至很难让用户使用该应用程序,因为搜索将花费几分钟(主要是加载))。
I want to hide some items based on a text filter in a ListView (WinForms).
Basically the listview loads the items from a text file, and I don't want it to be read and/or written when the user searches the list. The search is done in a combobox's KeyDown event, but there is no "Visible" property of the ListViewItem.
Is there any easy way to do this, WITHOUT re-reading the file?
(as it is an XML file, and it could even contain thousands of items, it would be hard to search efficiently and even let the user use the application, as the search would take for minutes (mostly with the loading)).
发布评论
评论(4)
由于每次添加/删除操作都会重新绘制列表框,您可能会遇到延迟。尝试将添加/删除操作包装在 Begin/End Update 方法中,如下所示。
现在感受一下速度。
当然,您只需加载文件一次。
You may be experiencing delay due to redrawing of listbox every add/remove operation. Try wrapping your Add/Remove op inside Begin/End Update method like this.
Now feel the speed.
Ofcourse you have to load your file only once.
我不清楚你想做什么。我仍然会大声说出一些想法,也许有些东西是有帮助的......
抱歉,如果我弄错了。 :(
It's not clear to me what you are trying to do. I'll still shout out a few ideas, maybe something is helpful...
Sorry if I got you wrong. :(
解决方法是创建一个单独的类来存储数据,并在加载文件后对其进行搜索。
虽然将更改写入文件仍然是一个问题,但那是另一个故事了......
Resolved with creating a separate class for storing data, and searching it after loading the file.
Although writing changes to the file is still a question, but that's another story...
这不是最好的答案,但算是我找到的最好的解决方案。您可以实施 ListView .DrawItem 事件自己做这样的事情:
然后重新排序所有项目,以便过滤后的项目位于 ListView 的末尾。这会有点隐藏它们,但 ListView 在计算其大小时仍会计算它们,因此您也应该强制调整 ListView 客户端大小。
This is not the best answer, But kinda the best solution I've found. You can implement ListView.DrawItem event yourself to do something like this:
Then reorder all items so that the filtered ones are in the end of ListView. This will kinda hide them, but ListView will still count them when computing it's size, So you should also forcefully resize ListView clientsize too.