Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted

try below
VB
Dim mylines() As String = ComboBox1.Items.Cast(Of String).ToArray
Dim count As integer = mylines.Where(Function(s) s.StartsWith("a")).Count()


Or without using LINQ
VB
Dim count As integer =0
For each item As String in myComboBox.Items
  If item.StartsWith("a") Then
    count += 1
  End If
Next
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 21-May-14 15:28pm    
5ed.
—SA
DamithSL 21-May-14 22:55pm    
thank you SA
Karen Mitchelle 22-May-14 0:57am    
5ed. :)
Well you could use a foreach loop...or Linq:
C#
string[] items = new string[myComboBox.Items.Count];
myComboBox.Items.CopyTo(items, 0);
int count = items.Where(i => i.StartsWith("a")).Count();
 
Share this answer
 
Comments
vidkaat 21-May-14 14:32pm    
Thanks for the reply. I get the error "Where is not a member of the System.array"
Sergey Alexandrovich Kryukov 21-May-14 15:29pm    
5ed.
—SA
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:

VB
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)

Note: Not tested!
 
Share this answer
 
v2
Comments
vidkaat 21-May-14 14:34pm    
Is there any other possibility of getting this done without LINQ
Sergey Alexandrovich Kryukov 21-May-14 15:28pm    
Why?!
And what do you think yourself? Isn't the answer obvious?
—SA
Sergey Alexandrovich Kryukov 21-May-14 15:28pm    
5ed.
—SA
Maciej Los 21-May-14 15:32pm    
Thank you, Sergey ;)
[no name] 24-May-14 20:42pm    
-5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900