Results 1 to 6 of 6

Thread: [RESOLVED] Search all items in a ComboBox based on a Search Criteria

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2012
    Location
    Los Angeles, CA
    Posts
    47

    Resolved [RESOLVED] Search all items in a ComboBox based on a Search Criteria

    I have three controls on my form; Textbox1, cboCity, lstCity
    When something (lets say "a") is entered in the TextBox1; I want to get each and every item in combobox cboCity that starts with "a" and add them to listbox lstCity.

    How do I do it?

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

    Re: Search all items in a ComboBox based on a Search Criteria

    How exactly was the ComboBox populated in the first place? Are the items just Strings added manually or have you maybe bound a DataTable or the like.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2012
    Location
    Los Angeles, CA
    Posts
    47

    Re: Search all items in a ComboBox based on a Search Criteria

    the combobox is unbound and filled manually (cboCity.items.Add(sCity))

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

    Re: Search all items in a ComboBox based on a Search Criteria

    In that case, your best bet is a simple LINQ query in the TextChanged event handler of the TextBox:
    vb.net Code:
    1. myListBox.DataSource = myComboBox.Items.Cast(Of String)().
    2.                                         Where(Function(s) s.StartsWith(myTextBox.Text)).
    3.                                         ToArray()
    Note that that will be case-sensitive. The StartsWith method also supports case-insensitivity, so check the documentation or even just use Intellisense if that's what you want.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Addicted Member
    Join Date
    Nov 2011
    Posts
    177

    Re: Search all items in a ComboBox based on a Search Criteria

    this is why i lurk in this forum.. *takes notes*

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2012
    Location
    Los Angeles, CA
    Posts
    47

    Re: Search all items in a ComboBox based on a Search Criteria

    Thanks... It worked great.
    I just had to add StringComparison.InvariantCultureIgnoreCase t o ignore the case.

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