Results 1 to 5 of 5

Thread: [RESOLVED] Combobox contains method question

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2018
    Location
    France, North
    Posts
    61

    Resolved [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:
    1. 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:
    1. Combobox1.items.contains("Name_Of_The_Item_Taped" & "*")
    OR
    VB.NET Code:
    1. 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 !

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    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:
    1. If ComboBox1.Items.
    2.              Cast(Of String)().
    3.              Any(Function(s) s.StartsWith(ComboBox1.Text)) Then

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    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:
    1. Dim isMatch = False
    2.  
    3. For Each s As String In ComboBox1.Items
    4.     If s.StartsWith(ComboBox1.Text) Then
    5.         isMatch = True
    6.         Exit For
    7.     End If
    8. Next
    9.  
    10. If isMatch Then

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2018
    Location
    France, North
    Posts
    61

    Re: Combobox contains method question

    Quote Originally Posted by jmcilhinney View Post
    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
  •  



Click Here to Expand Forum to Full Width