如何获取以'a'开头的组合框项目的计数 [英] How to get the count of the items of a combo box that starts with 'a'
本文介绍了如何获取以'a'开头的组合框项目的计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HI全部,
我有一个组合框,其值为a,a1,a2,b,b2。我如何获得以'a'开头的组合框的项目数。
请帮忙。
谢谢。
HI All,
I have a combo box with values like "a,a1,a2,b,b2". How do i get the count of the items of a combo box that starts with 'a'.
Please help.
Thanks.
推荐答案
在下面试试
try below
Dim mylines() As String = ComboBox1.Items.Cast(Of String).ToArray
Dim count As integer = mylines.Where(Function(s) s.StartsWith("a")).Count()
或者不使用LINQ
Or without using LINQ
Dim count As integer =0
For each item As String in myComboBox.Items
If item.StartsWith("a") Then
count += 1
End If
Next
您可以使用foreach
循环...或者Linq:
Well you could use aforeach
loop...or Linq:
string[] items = new string[myComboBox.Items.Count];
myComboBox.Items.CopyTo(items, 0);
int count = items.Where(i => i.StartsWith("a")).Count();
我建议使用 Linq [ ^ ]与 StartsWith [ ^ ]字符串方法。
看看这里: LINQ查询示例 [ ^ ]
这类似于:
I'd suggest to use Linq[^] query together with StartsWith[^] string method.
Have a look here: LINQ query examples[^]
It would be something similar to:
Dim itmscnt As Integer = (From itm As Object In ComboBox1.Items
Where (itm.ToString.StartsWith("a"))).Count
MsgBox("Count of items starting with 'a': " & itmscnt.ToString)
注意: 未经测试!
这篇关于如何获取以'a'开头的组合框项目的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文