title: How can I dynamically change auto complete entries in a C# combobox or textbox?
body: <p>I have a combobox in C# and I want to use auto complete suggestions with it, however I want to be able to change the auto complete entries as the user types, because the possible valid entries are far too numerous to populate the <code>AutoCompleteStringCollection</code> at startup.</p>
  <p>As an example, suppose I'm letting the user type in a name.  I have a list of possible first names (&quot;Joe&quot;, &quot;John&quot;) and a list of surnames (&quot;Bloggs&quot;, &quot;Smith&quot;), but if I have a thousand of each, then that would be a million possible strings - too many to put in the auto complete entries.  So initially I want to have just the first names as suggestions (&quot;Joe&quot;, &quot;John&quot;) , and then once the user has typed the first name, (&quot;Joe&quot;), I want to remove the existing auto complete entries and replace them with a new set consisting of the chosen first name followed by the possible surnames (&quot;Joe Bloggs&quot;, &quot;Joe Smith&quot;).  In order to do this, I tried the following code:</p>
  <pre><code>void InitializeComboBox()
  {
      ComboName.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
      ComboName.AutoCompleteSource = AutoCompleteSource.CustomSource;
      ComboName.AutoCompleteCustomSource = new AutoCompleteStringCollection();
      ComboName.TextChanged += new EventHandler( ComboName_TextChanged );
  }
  
  void ComboName_TextChanged( object sender, EventArgs e )
  {
      string text = this.ComboName.Text;
      string[] suggestions = GetNameSuggestions( text );
  
      this.ComboQuery.AutoCompleteCustomSource.Clear();
      this.ComboQuery.AutoCompleteCustomSource.AddRange( suggestions );
  }
  </code></pre>
  <p>However, this does not work properly.  It seems that the call to Clear() causes the auto complete mechanism to &quot;turn off&quot; until the next character appears in the combo box, but of course when the next character appears the above code calls Clear() again, so the user never actually sees the auto complete functionality.  It also causes the entire contents of the combo box to become selected, so between every keypress you have to deselect the existing text, which makes it unusable.  If I remove the call to Clear() then the auto complete works, but it seems that then the <code>AddRange()</code> call has no effect, because the new suggestions that I add do not appear in the auto complete dropdown.</p>
  <p>I have been searching for a solution to this, and seen various things suggested, but I cannot get any of them to work - either the auto complete functionality appears disabled, or new strings do not appear.  Here is a list of things I have tried:</p>
  <ul>
  <li>Calling <code>BeginUpdate()</code> before changing the strings and <code>EndUpdate()</code> afterward.</li>
  <li>Calling <code>Remove()</code> on all the existing strings instead of Clear().</li>
  <li>Clearing the text from the combobox while I update the strings, and adding it back afterward.</li>
  <li>Setting the <code>AutoCompleteMode</code> to &quot;None&quot; while I change the strings, and setting it back to &quot;SuggestAppend&quot; afterwards.</li>
  <li>Hooking the<code> TextUpdate</code> or <code>KeyPress</code> event instead of <code>TextChanged</code>.</li>
  <li>Replacing the existing <code>AutoCompleteCustomSource</code> with a new <code>AutoCompleteStringCollection</code> each time.</li>
  </ul>
  <p>None of these helped, even in various combinations.  <a href="https://stackoverflow.com/users/53771/spence">Spence</a> suggested that I try overriding the <code>ComboBox</code> function that gets the list of strings to use in auto complete.  Using a reflector I found a couple of methods in the <code>ComboBox</code> class that look promising - <code>GetStringsForAutoComplete()</code> and <code>SetAutoComplete()</code>, but they are both private so I can't access them from a derived class.  I couldn't take that any further.</p>
  <p>I tried replacing the <code>ComboBox</code> with a <code>TextBox</code>, because the auto complete interface is the same, and I found that the behaviour is slightly different. With the <code>TextBox</code> it appears to work better, in that the Append part of the auto complete works properly, but the Suggest part doesn't - the suggestion box briefly flashes to life but then immediately disappears.</p>
  <p>So I thought &quot;Okay, I'll live without the Suggest functionality and just use Append instead&quot;, however when I set the <code>AutoCompleteMode</code> to Append, I get an access violation exception. The same thing happens with Suggest - the only mode that doesn't throw exceptions is <code>SuggestAppend</code>, even though the Suggest part doesn't then behave correctly.</p>
  <p>I thought that it was supposed to be impossible to get access violation exceptions when using C# managed code.  <a href="https://stackoverflow.com/users/61883/avram">Avram</a> suggested I use &quot;lock&quot; to fix this, but I don't know what I should lock - the only thing that has a SyncRoot member is the <code>AutoCompleteStringCollection</code>, and locking that doesn't prevent the access violation exceptions. I also tried locking the <code>ComboBox</code> or <code>TextBox</code>, but that didn't help either. As I understand it, lock only prevents other locks, so if the underlying code isn't using lock then my using it won't make any difference.</p>
  <p>The upshot of all this is that I can't currently use a <code>TextBox</code> or a <code>ComboBox</code> with dynamic auto complete.  Does anyone have any insights into how I could achieve this?</p>
  <h3>Update:</h3>
  <p>I still haven't got this working, but I have found out some more.  Maybe some of this will inspire someone else to come up with a solution.</p>
  <p>I tried replacing the <code>ComboBox</code> with a <code>TextBox</code>, because the auto complete interface is the same, and I found that the behaviour is slightly different.  With the <code>TextBox</code> it appears to work better, in that the Append part of the auto complete works properly, but the Suggest part doesn't - the suggestion box briefly flashes to life but then immediately disappears.</p>
  <p>So I thought &quot;Okay, I'll live without the Suggest functionality and just use Append instead,&quot; however when I set the <code>AutoCompleteMode</code> to Append, I get an access violation exception.  The same thing happens with Suggest - the only mode that doesn't throw exceptions is <code>SuggestAppend</code>, even though the Suggest part doesn't then behave correctly.</p>
  <p>I thought that it was supposed to be impossible to get access violation exceptions when using C# managed code, but anyway, the upshot is that I can't currently use a <code>TextBox</code> or a <code>ComboBox</code> with any kind of dynamic auto complete.  Does anyone have any insights into how I could achieve this?</p>
  <h3>Update 2:</h3>
  <p>After trying various other things such as changing the autocomplete in a worker thread, and using <code>BeginInvoke()</code> to simulate PostMessage() type behaviour, I finally gave up and just implemented my own auto complete dropdown using a list box.  It's much more responsive than the built-in one, and I spent less time doing that than I did trying to get the built-in one to work, so the lesson for anyone else who wants this behaviour is - you're probably better off implementing it yourself.</p>