覆盖的WinForms组合框自动完成建议规则 [英] Override Winforms ComboBox Autocomplete Suggest Rule

查看:139
本文介绍了覆盖的WinForms组合框自动完成建议规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让自动完成按我指定的规则下拉显示项修改Windows.Forms的组合框的行为。

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.

在默认情况下,如果你在一个组合框使用自动完成,这是遵循规则是字符串s包含在下拉如果(s.StartsWith(userEnteredTextInTheComboBox))所有我在被代以新的规则很感兴趣当前一个,但我找不到任何办法得到它。 (具体而言,我想preFER s.Contains而不是s.StartsWith。)

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.

推荐答案

我有同样的问题,找了一个快速的解决方案。

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

最后,我结束了写它自己。这是一个有点脏,但它不应该是很难做,如果需要它prettier。

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

我们的想法是每个键preSS后重新搭建组合列表。这样我们就可以依靠组合的内置界面上,我们也不需要实现一个文本框和一个列表框我们自己的界面...

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...

不过,别忘了设置 combo.Tag 如果您重新构建组合的选项列表。

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());
}

这篇关于覆盖的WinForms组合框自动完成建议规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆