Override Winforms ComboBox Autocomplete Suggest a Rule

I'm trying to change the behavior of the Windows.Forms ComboBox package so that the autocomplete drop-down list displays items according to the specified rules.

By default, if you use AutoComplete in a ComboBox, the following rule: "string s is included in drop down if (s.StartsWith (userEnteredTextInTheComboBox))" All that interests me really is to replace the new rule with the current one, but I cannot find way to get to it. (In particular, I would prefer s.Contains instead of s.StartsWith.)

I can combine clumsy solutions using two controls instead of one, but I would really be happier with the one that really does what I want.

Update: I found essentially the same question after some searching. The answer posed there suggests that using two controls to "fake" is the way to go.

+13
c # autocomplete winforms combobox
Feb 13 '10 at 21:09
source share
2 answers

I had the same problem and was looking for a quick solution.

In the end, I finished writing myself. It's a bit dirty, but it shouldn't be difficult to make it prettier if necessary.

The idea is to rebuild the combo list after every keystroke. Thus, we can rely on the integrated combo interface, and we do not need to implement our own interface with a text field and a list ...

Remember to set combo.Tag to null if you are rebuilding the combo options list.

 private void combo_KeyPress(object sender, KeyPressEventArgs e) { comboKeyPressed(); } private void combo_TextChanged(object sender, EventArgs e) { if (combo.Text.Length == 0) comboKeyPressed(); } private void comboKeyPressed() { combo.DroppedDown = true; object[] originalList = (object[])combo.Tag; if (originalList == null) { // backup original list originalList = new object[combo.Items.Count]; combo.Items.CopyTo(originalList, 0); combo.Tag = originalList; } // prepare list of matching items string s = combo.Text.ToLower(); IEnumerable<object> newList = originalList; if (s.Length > 0) { newList = originalList.Where(item => item.ToString().ToLower().Contains(s)); } // clear list (loop through it, otherwise the cursor would move to the beginning of the textbox...) while (combo.Items.Count > 0) { combo.Items.RemoveAt(0); } // re-set list combo.Items.AddRange(newList.ToArray()); } 
+17
09 Oct
source share

Before Windows Vista, the Autocomplete object only matches candidates with a prefix , so you need to create your own .

If you need to reset the list of sentences when it is visible, use IAutoCompleteDropDown :: ResetEnumerator .

+2
Feb 22 2018-10-22
source share



All Articles