Odd combobox behaviour with Autocomplete

Martin Gierach 61 Reputation points
2021-01-22T10:31:26.777+00:00

Hi

I have a Windows Forms application which on the main screen has 2 comboboxes.

They are bound to separate datatables which are filled via a tableadapter (SQL) when the app loads. The binding is defined when the User Control is loaded - example for the first combo below:

With CboFirm

   BindingSourceFirms.DataSource = FirmsDT

   .DataSource = BindingSourceFirms
   .DisplayMember = "Company Name"
   .ValueMember = "Company Code"

 End With

In terms of the datatable filling process, the fill method is run in a BackgroundWorker and then I run BindingSourceFirms.ResetBindings(False) in the BackgroundWorker Completed event.

The data load side of things works fine.

Both combos have the AutoCompleteMode set to SuggestAppend and the AutoCompleteSource to ListItems.

What I am having issues with is when the app loads, and the user clicks on the first combobox and starts typing, the autocomplete doesn't work right away. The user has to click on the arrow in the combobox to expand the list, at which point all items show up and autocomplete starts working.

The second combobox is prefiltered based on the item selected in the first combo and the autocomplete works as expected.

What is even more bizarre is that everything work ok in the debug mode when I run it in VS but doesn't when the app is run on Windows.

Any ideas would be appreciated.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,884 questions
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,626 Reputation points
    2021-01-25T05:35:08.58+00:00

    Hi mrtn,
    >>In terms of the datatable filling process, the fill method is run in a BackgroundWorker and then I run BindingSourceFirms.ResetBindings(False) in the BackgroundWorker Completed event.
    This problem is probably caused by filling the DataTable in the secondary thread. Because it affects the ComboBox as a control, it cannot be accessed from a secondary thread. So you need to fill the unbound DataTable in the DoWork event handler, and then bind the data to the RunWorkerCompleted event handler, which is executed on the UI thread.
    Here is my test code:

    Friend Class SurroundingClass  
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)  
            backgroundWorker1.RunWorkerAsync()  
            comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend  
            comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems  
        End Sub  
      
        Private Sub backgroundWorker1_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)  
            Me.table1TableAdapter.Fill(Me.dataSet1.Table1)  
        End Sub  
      
        Private Sub backgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)  
            comboBox1.DataSource = dataSet1.Table1  
            comboBox1.DisplayMember = "ID"  
            comboBox1.ValueMember = "ID"  
        End Sub  
    End Class  
    

    Test result:
    60052-125.gif
    >>when I run it in VS but doesn't when the app is run on Windows.
    How Could you provide more information to reproduce the situation and what error did you encounter?
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.