如何筛选的基础上搜索的字符串列表框项目 [英] How to filter items in a ListBox based on a searched string
问题描述
我有一个Windows窗体,它包含了我增加了一些项目(我不使用一个数据源)列表框的应用程序(C#)。我想过滤ListBox中的项目,以显示包含字符串我在寻找的唯一项目。
我已经保持了原有的项目清单这样做,从该列表每次选择匹配的项目搜索字符串的变化和更新ListBox.Items
有没有更优雅/有效的方式来做到这一点?
有没有更优雅/有效的方式?
块引用>
没有,真的没有。
您可以通过一个BindingSource的连接,并且具有筛选和排序的属性,但是,这并不为工作一个简单的列表<>。所以,你将不得不使用像一个DataTable,而且不会是一种进步。
您当前的方法似乎不错,特别是如果你可以使用LINQ过滤列表。
不过,我希望你没有结束项目属性每次循环,刚分配过滤列表Listbox1.DataSource。
I have a Windows Forms application (C#) containing a ListBox into which I have added some items (I'm not using a DataSource). I want to filter the items in the ListBox to show only items containing a string I'm searching for.
I have done this by keeping a list of the original items and selecting matching items from that list each time the search string changes and updating the ListBox.Items
Is there a more elegant/efficient way to do this?
解决方案Is there a more elegant/efficient way?
No, not really.
You could connect through a BindingSource and that has Filter and Sort properties, but that doesn't work for a simple List<>. So you would have to use something like a DataTable and that would not be an improvement.
Your current method seems fine, especially if you can use LINQ to filter the list.
But I hope you're not looping over the Items property each time, just assign the filtered list to Listbox1.DataSource.
这篇关于如何筛选的基础上搜索的字符串列表框项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!