|
-
Apr 12th, 2019, 08:27 AM
#1
Thread Starter
Member
[RESOLVED] Combobox contains method question
Hi, in my VB.NET app, I'm using a combobox.
I want to check if the item which is taped in the combobox is existing in the items collection of the combobox.
I seen the method contains called like :
VB.NET Code:
Combobox1.items.contains("Name_Of_The_Item_Taped")
But I don't want to check the exact string, just the beginning, someting like that :
VB.NET Code:
Combobox1.items.contains("Name_Of_The_Item_Taped" & "*")
OR
VB.NET Code:
Combobox1.items.contains( Like "Name_Of_The_Item_Taped *")
As you see, I don't know how to write it , if you have any idea, I'm listening !
Thank you by advance !
-
Apr 12th, 2019, 08:45 AM
#2
Re: Combobox contains method question
I think you mean "typed" rather than "taped". The text entered by the user can be accessed using the Text property.
vb.net Code:
If ComboBox1.Items. Cast(Of String)(). Any(Function(s) s.StartsWith(ComboBox1.Text)) Then
-
Apr 12th, 2019, 08:46 AM
#3
Re: Combobox contains method question
The Contains method of a Collection (such as Items in this case) can only find whole items, for anything else it is better to loop, eg:
Code:
Dim doesContain as Boolean = False
For Each item as Object In Combobox1.items
If item.ToString().StartsWith("Name_Of_The_Item_Taped") Then
doesContain = True
Exit For
End If
Next
If doesContain Then
...
Edit: beaten to it!
Note that this method and jmcilhinney's do the same thing, just using different techniques.
Last edited by si_the_geek; Apr 12th, 2019 at 08:50 AM.
-
Apr 12th, 2019, 08:48 AM
#4
Re: Combobox contains method question
That code uses LINQ, which is basically a way to compress loops, and is equivalent to this:
vb.net Code:
Dim isMatch = False For Each s As String In ComboBox1.Items If s.StartsWith(ComboBox1.Text) Then isMatch = True Exit For End If Next If isMatch Then
-
Apr 15th, 2019, 03:21 AM
#5
Thread Starter
Member
Re: Combobox contains method question
 Originally Posted by jmcilhinney
I think you mean "typed" rather than "taped".[/HIGHLIGHT]
yes you are right, I'm sorry for that mistake !
Sorry for the delay (Weekend time), I tried this morning your code @jmcilhinney and this is great !
You both saved a lot of my time, thank you for your answers ! :
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|