Results 1 to 11 of 11

Thread: [RESOLVED] how to search in a combobox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2015
    Posts
    171

    Resolved [RESOLVED] how to search in a combobox

    I've a combobox to display names:
    Code:
      Dim names As New List(Of String)
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            names.Add("John")
            names.Add("Paul")
            names.Add("Lea")
            names.Add("Jen")
            names.Add("Joan")
            names.Add("Alex")
            names.Add("Jonny")
            names.Add("Alexis")
            names.Add("Francis")
            cmbNames.DataSource = names
        End Sub
    When user digit on the combobox I would to filter and show only the names that contains the string entered.
    So in textchange event I tryed to use .Contains and .findString, but I not get the behavour expected:
    if "is" digit, I would have Alexis and Francis as result, if "Jo" is entered it would get John, Jonny and Joan. Instead I still get only the result that start with the string searched.
    Any advice?

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

    Re: how to search in a combobox

    Why not use the autocomplete functionality built into the ComboBox control?

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

    Re: how to search in a combobox

    BTW, you can do this:
    vb.net Code:
    1. Dim names As New List(Of String) From {"John", "Paul", ..., "Francis"}

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2015
    Posts
    171

    Re: how to search in a combobox

    If you mean by:
    Code:
    AutoCompleteMode=SuggestAppend
    AutoCompleteSource=ListItems
    it give me ony the names that start with the entered string, not all the name that contain it.
    But maybe I misconfigured it.

  5. #5
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    Re: how to search in a combobox

    It should definitely be
    Code:
    ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems
    for latter property. But neither for none of
    Code:
    ComboBox1.AutoCompleteMode = AutoCompleteMode.Append 'Autofills, No combo opens
    ComboBox1.AutoCompleteMode = AutoCompleteMode.Suggest 'No autofills, Combo opens
    ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend 'Autofills, Combo opens
    these, for a list of
    Code:
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim NamesList As New List(Of String) From {"John Johnson", "Paul McCartney Junior", "Sir Francis Ford Coppola"}
        ComboBox1.DataSource = NamesList
    End Sub
    by pressing only one 'j' you expect that a combo opens with following items but unfortunately you will not get

    . John Johnson
    . Paul McCartney Junior


    (Ignore colors. I tried 4 words names containing 3 'j's in total for a complex example)
    as result.

    As far as I know ListBox1.FindString will also do the same, so it is also a rejected. What are you looking for can be achieved by a sort of seeking loop that specific entered string in whole string of every item.
    Try another way.
    Last edited by pourkascheff; Jun 6th, 2023 at 03:32 PM. Reason: Color grading for better understanding.

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

    Re: how to search in a combobox

    Quote Originally Posted by hannibal smith View Post
    it give me ony the names that start with the entered string, not all the name that contain it.
    You are correct. Sorry, I should have read your question more carefully.

  7. #7
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: how to search in a combobox

    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: how to search in a combobox

    You can try something like this:
    Code:
    Public Class Form2
    
        Private ReadOnly _names As List(Of String)
    
        Sub New()
            InitializeComponent()
    
            _names = New List(Of String) From {
                "John",
                "Paul",
                "Lea",
                "Jen",
                "Joan",
                "Alex",
                "Jonny",
                "Alexis",
                "Francis"
            }
            cmbNames.Items.AddRange(_names.ToArray())
            cmbNames.AutoCompleteMode = AutoCompleteMode.None
        End Sub
    
        Private Sub cmbNames_TextChanged(sender As Object, e As EventArgs) Handles cmbNames.TextChanged
            Dim caretPosition = cmbNames.SelectionStart
            cmbNames.Items.Clear()
    
            Dim input = cmbNames.Text
            Dim matches = _names.AsEnumerable()
            If (Not String.IsNullOrWhiteSpace(input)) Then
                matches = matches.Where(Function(item) item.IndexOf(input, StringComparison.OrdinalIgnoreCase) > -1)
            End If
    
            cmbNames.Items.AddRange(matches.ToArray())
            cmbNames.SelectionStart = caretPosition
        End Sub
    
    End Class
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jun 2015
    Posts
    171

    Re: how to search in a combobox

    I found out that a third part control with the feature that I need already exist (sfCombobox).

  10. #10
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    Re: [RESOLVED] how to search in a combobox

    Good to hear that but - as an archive record - it might be implemented with like query as we made to lookup in databases, (I don't know how). A also believe in certified 3rd party controls.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jun 2015
    Posts
    171

    Re: [RESOLVED] how to search in a combobox

    Yes you are right

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