Our WinForms application does lazy loading of the data for auto complete of a textbox. The pseudocode for this is as follows;
- User types in TextBox
- On typing pause, determine if we need to fetch the auto complete data
- In worker thread, contact the server and fetch data
- Invoke back to the UI thread
- Set
textBox.AutoCompleteCustomSource = fetchedAutoCompleteStringCollection;
- 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.