Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override Winforms ComboBox Autocomplete Suggest Rule

I'm trying to modify the behaviour of a Windows.Forms ComboBox so that the AutoComplete drop down displays items according to the rules I specify.

By default, if you use AutoComplete in a ComboBox, the rule that's followed is "string s is included in the drop down if( s.StartsWith( userEnteredTextInTheComboBox) )" All I'm really interested in is substituting a new rule for the current one, but I can find no way to get at it. (Specifically, I'd prefer s.Contains instead of s.StartsWith.)

I can kludge together a clumsy solution using two controls instead of one, but I'd really be happier with one that actually does what I want.

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

like image 382
user272551 Avatar asked Feb 13 '10 21:02

user272551


People also ask

How do I enable autocompletemode in WinForms combobox?

AutoCompleteMode = AutoCompleteMode. Suggest The closest match will be added to the editor portion of the WinForms ComboBox (SfComboBox) control with partial string selection in the drop-down list. This mode can be enabled by setting the AutoCompleteMode property as Append.

What is autocompletemode = suggest with autocompletesource = listitems?

The WinForms ComboBox-Control offers a functionality called AutoComplete if is set to DropDown. There are different options available, but the focus of this article is on AutoCompleteMode = Suggest with AutoCompleteSource = ListItems. This setting provides a ListBox of suggested items when you type some text in the ComboBox.

Is it possible to use combobox for auto complete?

Bear in mind that it does not use combobox's auto-complete features, and it might be quite slow if you use it to sift thru a lot of items... Show activity on this post. I've found Max Lambertini's answer very helpful, but have modified his HandleTextChanged method as such: Show activity on this post. This code is write on your form load.

How do I create a combobox that automatically completes input strings?

Use the AutoCompleteCustomSource, AutoCompleteMode, and AutoCompleteSource properties to create a ComboBox that automatically completes input strings by comparing the prefix being entered to the prefixes of all strings in a maintained source.


2 Answers

I've had the same problem and looked for a quick solution.

Eventually I ended up writing it myself. It's a little dirty but it should not be hard to make it prettier if needed.

The idea is to re-build the combo list after every key press. This way we can rely on the combo's built-in interface, and we don't need to implement our own interface with a textbox and a listbox...

Just remember to set combo.Tag to null if you re-build the combo's 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());
}
like image 90
obe Avatar answered Sep 21 '22 13:09

obe


Before Windows Vista, the Autocomplete object match candidates with prefix only, so you need to cook your own.

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

like image 31
Sheng Jiang 蒋晟 Avatar answered Sep 18 '22 13:09

Sheng Jiang 蒋晟