ComboBox DropDownList搜索文本 [英] ComboBox DropDownList searching for text

查看:152
本文介绍了ComboBox DropDownList搜索文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ComboBox DataSource 属性设置为此类型的列表

I have a ComboBox the DataSource property is set to a List of this Type:

public class ListInfo
{
    public int Key { get; set; }
    public string Name { get; set; }
}

DropDownStyle 设置为 DropDownList ,我将 AutoCompleteSource 设置为 ListItems AutoCompleteMode SuggestAppend

The DropDownStyle is set to DropDownList, I set the AutoCompleteSource to ListItems and the AutoCompleteMode to SuggestAppend.

客户端回来并要求能够找到文本值的任何部分,而不仅仅是从文本的开始。我看到的大多数例子是这样的,当 DropDownStyle 设置为 DropDown ,我不能这样做,用户不能编辑列表的内容只是选择一个值。

After some testing the client has come back and asked to be able to find any part of the text value, not just from the start of the text. Most examples that I have seen do this when the DropDownStyle is set to DropDown, I can't do that because the user cannot edit the contents of the list just select a value.

我已经尝试创建一个 CustomSource 当我尝试将 AutoCompleteMode 设置为任何值时,我得到以下消息:

I have tried to create a CustomSource, but when I try to set the AutoCompleteMode to any value I get the following message:


只有当DropDownStyle为
时,才可以使用值AutoCompleteMode.None ComboBoxStyle.DropDownList和AutoCompleteSource不是
AutoCompleteSource.ListItems。

Only the value AutoCompleteMode.None can be used when DropDownStyle is ComboBoxStyle.DropDownList and AutoCompleteSource is not AutoCompleteSource.ListItems.

我发现这个 AutoSuggestCombo ,与 DropDownStyle

我如何:


  1. 使用 ComboBox DropDownStyle 设置为 DropDown ,不允许end

  1. Use the ComboBox with the DropDownStyle set to DropDown, that doesn't allow end user to enter new elements?

这是开始使用Rx的机会,还是路由一个blo肿的解决方案和学习曲线自带了吗? (到目前为止使用的简单教程)

Is this the opportunity to get started with Rx, or is that route a bloated solution and the learning curve that comes with it? (used simple tutorials so far)

推荐答案

您必须将所有自动完成属性设置为无,
可能有更简单的解决方案,但您可以像这样编码KeyPress事件。

You must set all Autocompletion properties to none and handle the stuff yourself. There could be simpler solutions, but you can code the KeyPress event like this.

private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    SortedDictionary<int, ListInfo> dict = new SortedDictionary<int, ListInfo>();

    int found = -1;
    int current = comboBox1.SelectedIndex;

    // collect all items that match:
    for (int i = 0; i < comboBox1.Items.Count; i++)
        if (((ListInfo)comboBox1.Items[i]).Name.ToLower().IndexOf(e.KeyChar.ToString().ToLower()) >= 0)
        // case sensitive version:
        // if (((ListInfo)comboBox1.Items[i]).Name.IndexOf(e.KeyChar.ToString()) >= 0)
                dict.Add(i, (ListInfo)comboBox1.Items[i]);

    // find the one after the current position:
    foreach (KeyValuePair<int, ListInfo> kv in dict)
             if (kv.Key > current) { found = kv.Key; break; }

    // or take the first one:
    if (dict.Keys.Count > 0 && found < 0) found = dict.Keys.First();

    if (found >= 0) comboBox1.SelectedIndex = found;

    e.Handled = true;

}

您可以决定是否要区分大小写;可能不是..

You can decide if you want case sensitivity; probably not..

这篇关于ComboBox DropDownList搜索文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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