简体   繁体   English

计算ComboBox的DropDown部分中匹配的建议项的数量

[英]Count the number of matching suggested items in the DropDown portion of a ComboBox

In my ComboBox I set: 在我的组合框中设置:

  • datasource = [bindingsource]
  • DisplayMember and ValueMember DisplayMemberValueMember
  • AutoCompleteSource = ListItems
  • AutoCompleteMode = Suggest

How can I get the number of suggested items in the DropDown portion, after entering text in the ComboBox? 在ComboBox中输入文本后,如何在DropDown部分中获得建议项的数量?

For example, I type "how" and the DropDown list suggests 5 matching items. 例如,我键入“ how”,并且“ DropDown列表建议5个匹配项。 How can I grab this number? 我怎样才能拿到这个号码?

I don't see any property on the ComboBox control that gives you access to that information. 我在ComboBox控件上看不到任何可让您访问该信息的属性。

However, you should be able to do the same search against your ComboBox's DataSource that the ComboBox is doing internally, using a little LINQ: 但是,您应该能够使用一些LINQ对ComboBox的数据源执行与ComboBox内部进行的相同搜索:

var matches = comboBox1.Items.Cast<DataRowView>()
                       .Count(x => Convert.ToString(x["SomeColumn"])
                                          .StartsWith(comboBox1.Text))

Add the following using directive to the top of your class, if it's not there already: 如果尚不存在,请在类顶部添加以下using指令:

using System.Linq;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM