简体   繁体   English

高级项目搜索 combobox C#

[英]Advanced item search in combobox C#

I wrote a program with C # I have a combo box whose items are Binding from the database.I use AutoCompleteMode and AutoCompleteSource to search the combo box.But only when filtering does it find words whose first letter is the same as the input letter.While I need All items that contain these letters displayed.Is there a solution to this problem?我用 C 编写了一个程序 # 我有一个组合框,其项目是从数据库中绑定的。我使用 AutoCompleteMode 和 AutoCompleteSource 来搜索组合框。但是只有在过滤时它才会找到首字母与输入字母相同的单词。虽然我需要显示包含这些字母的所有项目。这个问题有解决方案吗?

maybe this helps也许这有帮助

// Example data 
string[] data = new string[] {
    "Absecon","Abstracta","Abundantia","Academia","Acadiau","Acamas",
    "Ackerman","Ackley","Ackworth","Acomita","Aconcagua","Acton","Acushnet",
    "Acworth","Ada","Ada","Adair","Adairs","Adair","Adak","Adalberta","Adamkrafft",
    "Adams"

};
public Form1()
{
    InitializeComponent();
}

private void comboBox1_TextChanged(object sender, EventArgs e)
{
    HandleTextChanged();
}

// Handle Text Box that you Fill
private void HandleTextChanged()
{
    var txt = comboBox1.Text;
    var list = from d in data
               where d.Tolower().Contains(comboBox1.Text.ToLower())
               select d;
    if (list.Count() > 0)
    {
        comboBox1.DataSource = list.ToList();
        //comboBox1.SelectedIndex = 0;
        var sText = comboBox1.Items[0].ToString();
        comboBox1.SelectionStart = txt.Length;
        comboBox1.SelectionLength = sText.Length - txt.Length;
        comboBox1.DroppedDown = true;
        return;
    }
    else
    {
        comboBox1.DroppedDown = false;
        comboBox1.SelectionStart = txt.Length;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM