views:

457

answers:

1

Our WinForms application does lazy loading of the data for auto complete of a textbox. The pseudocode for this is as follows;

  1. User types in TextBox
  2. On typing pause, determine if we need to fetch the auto complete data
  3. In worker thread, contact the server and fetch data
  4. Invoke back to the UI thread
  5. Set textBox.AutoCompleteCustomSource = fetchedAutoCompleteStringCollection;
  6. Force the textbox to drop down it's autocomplete dropdown.

I am currently having trouble with #6. As a hack, I do the following to simulate a keypress which works, but it does not work in all situations.

     // This is a hack, but the only way that I have found to get the autocomplete
     // to drop down once the data is returned.
     textBox.SelectionStart = textBox.Text.Length;
     textBox.SelectionLength = 0;
     SendKeys.Send( " {BACKSPACE}" );

There must be a better way. I can't believe that I am the only person fetching auto complete data asynchronously. How should I be doing this?

EDIT: A Win32 call to cause the Auto Complete to dropdown would be acceptable. I don't mind PInvoking out if I have to.

+1  A: 

Typically, you would use COM interop and access the IAutoComplete, IAutoComplete2, or the IAutoCompleteDropDown interface. Unfortunately, none of these has methods to force the autocomplete to drop down.

You might want to use Spy++ and look at the windows messages that are being sent to the control when the auto complete displays. You might find a command message which will activate it. Of course, this is an implementation detail, but it might be the only way to go here.

casperOne