Hello!
My question is how to code search in combobox each time typeing a letter. And the search result in DropDown have to show list items which contains letter or the continues number of letters. (Not only ... starts with the inputed letter/s/).
Thanks.

Recommended Answers

All 8 Replies

Imports System.Collections
Imports System.Linq
Public Class searchInComboBox
    Public aC As New ArrayList
    Private Sub searchInComboBox_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Try
            aC.AddRange(cboxDomains.Items.Cast(Of String).ToList())
        Catch ex As Exception
        End Try
    End Sub

    Private Sub cboxDomains_TextUpdate(sender As Object, e As System.EventArgs) Handles cboxDomains.TextUpdate
        Try
            Dim query = From str As String In aC Where
            str Like "*" + sender.text + "*"
            cboxDomains.Items.Clear()
            cboxDomains.Items.AddRange(query.ToArray)
            cboxDomains.DroppedDown = True
            cboxDomains.Select(Len(cboxDomains.Text), 0)
        Catch ex As Exception
        End Try
    End Sub
End Class
Here, autocomplete is not used and the list is in the combobox item property. On the load event the list is copied into the `aC`ArrayList and, then, on the TextUpdate event Linq does the work, selecting only those that verify the `Like`condition. 

cbox1.PNG cbox2.PNG

    Private Sub cboxDomains_TextUpdate(sender As Object, e As System.EventArgs) Handles cboxDomains.TextUpdate

        Try
            Dim txt As String = cboxDomains.Text
            Dim query = From str As String In aC Where
            str Like "*" + sender.text + "*"
            cboxDomains.Items.Clear()
            cboxDomains.Focus()
            cboxDomains.Items.AddRange(query.ToArray)
            cboxDomains.Select(Len(txt), 1)
            cboxDomains.DroppedDown = True
        Catch ex As Exception
        End Try
    End Sub

cbox3.PNG Click Here

Imports System.Collections
Imports System.Linq

Public Class searchInComboBox

    Public aC As New ArrayList
    Private Sub searchInComboBox_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Try
            aC.AddRange(cboxDomains.Items.Cast(Of String).ToList())
        Catch ex As Exception
        End Try
    End Sub

    Private Sub cboxDomains_TextUpdate(sender As Object, e As System.EventArgs) Handles cboxDomains.TextUpdate
        Try
            With cboxDomains
                Dim query = From str As String In aC Where
                str Like "*" + .Text + "*"
                .Items.Clear()
                .Focus()
                .Items.AddRange(query.ToArray)
                Dim em As New MouseEventArgs( _
                    Windows.Forms.MouseButtons.Left, 1, 1, 1, 0)
                Cursor.Position = New Point( _
                    Me.Location.X + .Location.X + .Width / 2, _
                    Me.Location.Y + .Bottom)
                .Select(Len(.Text), 1)
            End With
            SendKeys.SendWait("%{down}{tab}")
        Catch ex As Exception
        End Try
    End Sub

End Class

I am sending the code again, because there was a problem with the cursor not showing when located outside of the form. Now, it seems solved.

cbox4.PNG

Of course instruction Dim em As New MouseEventArgs... does nothing: you may remove it. I am sorry for it.

Thanks for your answers. The 3-rd Code is almost work but not quite. The problem is when typing in combobox it accepts every second click. I was tryed figuere that out, but no success. And unfortunately the mouse at some point disappear. Can u doble checked for this? ... 10x again !

To avoid the mouse disappearing is what for SendKeys instruction stands. I have checked again and for me the code is working fine: when writing in the keyboard the search pattern, the mouse moves near the combobox, the dropdown shows the items meeting the 'Like' condition and the mouse remains visible. So here goes another time the code hopping it's ok and has no extra code (the {tab}had no need to be there):

Imports System.Collections
Imports System.Linq

Public Class searchInComboBox

    Public aC As New ArrayList
    Private Sub searchInComboBox_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Try
            aC.AddRange(cboxDomains.Items.Cast(Of String).ToList())
        Catch ex As Exception
        End Try
    End Sub

    Private Sub cboxDomains_TextUpdate(sender As Object, e As System.EventArgs) Handles cboxDomains.TextUpdate
        Try
            With cboxDomains
                Dim query = From str As String In aC Where
                str Like "*" + .Text + "*"
                .Items.Clear()
                .Focus()
                .Items.AddRange(query.ToArray)
                Cursor.Position = New Point( _
                    Me.Location.X + .Location.X + .Width / 2, _
                    Me.Location.Y + .Bottom)
                .Select(Len(.Text), 1)
            End With
            SendKeys.SendWait("%{down}")
        Catch ex As Exception
        End Try
    End Sub

End Class

...and please let me know if there's any problem giving detailed info of the steps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.