2

How can I extend the AutoComplete feature of a WinForm ComboBox to, for example, find matches of items using a regular expression or for simplicity to find items that contains the text entered?

dtb
  • 213,145
  • 36
  • 401
  • 431
Bassl
  • 21
  • 1
  • 2

2 Answers2

4

The AutoCompleteMode in Windows Forms does not (as of now) support filtering with regular expression or matching from the middle. It supports prefix filtering only.

The API to reset the autocomplete options as you type is IAutoCompleteDropDown::ResetEnumerator. You need to call it in the TextChanged event. On Windows Vista or later you can call IAutoComplete2::SetOptions with ACO_NOPREFIXFILTERING to disable prefix filtering.

Sheng Jiang 蒋晟
  • 15,125
  • 2
  • 28
  • 46
  • could you provide an example? – Mike Tsayper Jan 14 '20 at 13:48
  • 1
    see https://social.msdn.microsoft.com/Forums/windows/en-US/7bf322cf-8b54-4aa7-b813-a76899a783f1/auto-complete-text-box-for-setting-tags-c?forum=winforms – Sheng Jiang 蒋晟 Jan 14 '20 at 18:00
  • Sheng Jiang 蒋晟, i have set the NONPREFIX option successfully (0x100) but calling autoCompleteObject2.ResetEnumerator() raises AccessViolation. I've added Int32 ResetEnumerator(); to the interface. How to fix it? – Mike Tsayper Jan 15 '20 at 05:58
  • i have set the NONPREFIX option successfully (0x100) but calling autoCompleteObject2.ResetEnumerator() raises AccessViolation. I've added Int32 ResetEnumerator(); to the interface. How to fix it? – Mike Tsayper Jan 15 '20 at 06:06
  • that may or may not be right, depending on whether you have the preservesig attribute. – Sheng Jiang 蒋晟 Jan 17 '20 at 02:09
2

For finding the existing list item that best matches what the user has typed, you can set the AutoCompleteMode property on the ComboBox to AutoCompleteMode.Append and the AutoCompleteSource to AutoCompleteSource.ListItems.

If you want to use more complex logic to perform the auto-complete, look at doing something with the TextChanged event. An Example

Ben Gribaudo
  • 5,057
  • 1
  • 40
  • 75