[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 823
  • Last Modified:

Asynchronously Load Items to Combobox autocomplete source as user types.

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

1 Solution
Jacques Bourgeois (James Burger)PresidentCommented:
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

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now