覆盖 Winforms ComboBox 自动完成建议规则 [英] Override Winforms ComboBox Autocomplete Suggest Rule

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

问题描述

我正在尝试修改 Windows.Forms ComboBox 的行为,以便自动完成下拉列表根据我指定的规则显示项目.

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.

默认情况下,如果您在 ComboBox 中使用 AutoComplete,则遵循的规则是字符串 s 包含在下拉列表中 if( s.StartsWith( userEnteredTextInTheComboBox) )";我真正感兴趣的是用新规则代替当前规则,但我找不到办法解决它.(具体来说,我更喜欢 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.

最后是我自己写的.它有点脏,但如果需要的话,让它变得更漂亮应该不难.

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

这个想法是在每次按键后重新构建组合列表.这样我们就可以依赖combo的内置接口了,不需要用文本框和列表框来实现自己的接口...

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 设置为 null.

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 ComboBox 自动完成建议规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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