基于过滤器隐藏ListViewItem

发布于 2024-09-02 21:06:10 字数 251 浏览 9 评论 0 原文

我想在 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)).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

七颜 2024-09-09 21:06:10

由于每次添加/删除操作都会重新绘制列表框,您可能会遇到延迟。尝试将添加/删除操作包装在 Begin/End Update 方法中,如下所示。

myListView.BeginUpdate();
//Add or Remove Items
myListView.EndUpdate();

现在感受一下速度。

当然,您只需加载文件一次。

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.

myListView.BeginUpdate();
//Add or Remove Items
myListView.EndUpdate();

Now feel the speed.

Ofcourse you have to load your file only once.

〆一缕阳光ご 2024-09-09 21:06:10

我不清楚你想做什么。我仍然会大声说出一些想法,也许有些东西是有帮助的......

  • 将文件缓存在内存中
  • 缓存读取的项目并动态填充输入框
  • 相应地添加和删除项目
  • 看看这个问题
  • 也许是不可能毕竟?

抱歉,如果我弄错了。 :(

It's not clear to me what you are trying to do. I'll still shout out a few ideas, maybe something is helpful...

  • Cache the file in memory
  • Cache the read items and fill the input box on the fly
  • Add and remove the items accordingly
  • Have a look at this question
  • Maybe it's impossible after all?

Sorry if I got you wrong. :(

浪荡不羁 2024-09-09 21:06:10

解决方法是创建一个单独的类来存储数据,并在加载文件后对其进行搜索。

虽然将更改写入文件仍然是一个问题,但那是另一个故事了......

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...

贪了杯 2024-09-09 21:06:10

这不是最好的答案,但算是我找到的最好的解决方案。您可以实施 ListView .DrawItem 事件自己做这样的事情:

    void listView_DrawItem(object sender, DrawListViewItemEventArgs e)
    {               
        if (Should_Filter(e.Item) == false)
            e.DrawDefault = true;                   
        else
            e.DrawDefault = false;
    }

然后重新排序所有项目,以便过滤后的项目位于 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:

    void listView_DrawItem(object sender, DrawListViewItemEventArgs e)
    {               
        if (Should_Filter(e.Item) == false)
            e.DrawDefault = true;                   
        else
            e.DrawDefault = false;
    }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文