karephul
karephul

Reputation: 1483

Autocomplete [contains instead of starting with] in winform TextBox

// [in designer] textBoxInContext.AutoCompleteMode = Suggest
// [in designer] textBoxInContext.AutoCompleteSource = CustomSource
AutoCompleteStringCollection autoComplete = new AutoCompleteStringCollection();
autoComplete.AddRange(myArrayofStrings);
textBoxInContext.AutoCompleteCustomSource = autoComplete;

I have this code which works well as documented in MSDN.

Problem: if user types "PS" it shows all the string starting with "PS"; I would like to display all the strings containing "PS"

Any pointers ?

Upvotes: 16

Views: 9241

Answers (2)

David
David

Reputation: 43

If you don't find another way, I suggest doing it manually:

  1. Use a combobox with no items(you'll fill them manually later).
  2. Have a string array with your possible suggestions.
  3. At the combobox.TextChanged or KeyUp event take its text and compare it to your string array whichever way you want and, after clearing the combobox.Items, add the found results to the combobox.Items and make sure to set the DroppedDown property to true if you have found suggestions.

Upvotes: 2

sq33G
sq33G

Reputation: 3360

The stupid but fun suggestion: make a class that inherits from AutoCompleteStringCollection and play with it in debug to see if you can fake this out.

The normal suggestion: make your own autocomplete with a listbox.

Upvotes: 0

Related Questions