Asynchronously Load Items to Combobox autocomplete source as user types.

Posted on 2011-10-30
Last Modified: 2012-05-12
In my example I trying the following:

As the user types text into the combobox -> I get the text on the textchanged event.

Run that text through a background worker and bind the results to the combobox.

What I am tyring to implement is like a web callback service where I can async load data from a table and suggest.

If I bind the data to the combobox, evrything goes wrong and my text resets.

I am now trying to add the items to the AutoCompleteSource of the combobox.

My Code is below:

What I need is for the combobox to dropdown. But I donot want it autocomplete.

Am I doing it right?
 Any other ways to implement?

Private Sub ComboBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.TextChanged
        query = String.Format("Select TOP 100 cn_id,cn_fname +' '+cn_lname as cn_fname from tblcandidate where cn_fname like '{0}%' ", ComboBox1.Text)
        BGLoader = New BackgroundWorker() With {.WorkerReportsProgress = True, .WorkerSupportsCancellation = True}
        Label1.Visible = True
    End Sub
    Private Sub BGLoader_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BGLoader.DoWork
        Dim dt As New DataTable
        Dim da As New SqlDataAdapter
        Dim query As String
        If e.Argument IsNot Nothing Then
            query = DirectCast(e.Argument, String)
        End If

        da = New SqlDataAdapter(query, conns)
        e.Result = dt
    End Sub
    Private Sub BGLoader_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BGLoader.RunWorkerCompleted
        If e.Error IsNot Nothing Then

            Exit Sub
        End If
        If e.Cancelled = False Then
            If e.Result IsNot Nothing Then
                Dim DTData As DataTable = DirectCast(e.Result, DataTable)

                ''Code Add items to the combobox AutoComplete source

                Label1.Visible = False
            End If
        End If
    End Sub

Open in new window

Question by:isaackhazi
    1 Comment
    LVL 40

    Accepted Solution

    When you say that you add the items to the AutoCompleteSource, I suppose you mean the AutoCompleteStringCollection. You cannot add to the AutoCompleteSource property.

    If you want to do it while the user is typing, you would have to requery the database, refresh the list and possibly bring back the selection to what you want to be the selection at that point. I have no clue as to what the effect of changing the AutoCompleteCollection could do while the user is typing. You might need to have to inherit from the ComboBox class and override some of the events to prevent them from being triggered.

    Unless you retrieve your data from a fast locally installed database, most users would type faster than your routine could work, so you would end up either with a painful synchronization issue or a very awkward interface for the user. Probably both. This is even worst since you are using a DataAdapter and a DataTable to retrieve the information. A DataReader would be faster. Asynchrone does not mean faster. In fact, it often slows down some operations because the system needs to switch continuously between 2 threads.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now