【问题标题】:How to Create combo box to auto fill while user type the spelings inside the combo Box in c#如何在用户在 C# 的组合框中键入拼写时创建组合框以自动填充
【发布时间】:2013-03-05 08:38:17
【问题描述】:

我的朋友们,我的 windows 窗体中有一个组合框,我可以用数据库中的数据填充它, 但是当用户在组合框内输入字母时,我无法填充组合框, 例如,当用户在组合框旁边键入字母“R”时,组合框必须下拉并显示所有可能的字母“R”

【问题讨论】:

标签: c# winforms combobox


【解决方案1】:
  1. yourComboBox.AutoCompleteSource 设置为AutoCompleteSource.ListItems;(如果您的yourComboBox.Items 已从数据库中填充)
  2. yourComboBox.AutoCompleteMode 设置为SuggestAppend

【讨论】:

  • 感谢朋友帮了我很多
【解决方案2】:

您必须与组合框上的 KeyUp 事件相关联,并使用 comboBox.Text 过滤 comboBox.Items 集合以仅显示包含键入的字符。您还需要强制组合框窗口下拉。

【讨论】:

    【解决方案3】:

    希望对您有所帮助:

    private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        char ch = e.KeyChar;
        string strToFind;
    
        // if first char
        if (lastChar == 0)
            strToFind = ch.ToString();
        else
            strToFind = lastChar.ToString() + ch;
    
        // set first char
        lastChar = ch;
    
        // find first item that exactly like strToFind
        int idx = comboBox1.FindStringExact(strToFind);
    
        // if not found, find first item that start with strToFind
        if (idx == -1) idx = comboBox1.FindString(strToFind);
    
        if (idx == -1) return;
    
        comboBox1.SelectedIndex = idx;
    
        e.Handled = true;
    }
    
    void comboBox1_GotFocus(object sender, EventArgs e)
    {
        // remove last char before select new item
        lastChar = (char) 0;
    }
    

    来自here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      • 2013-07-25
      相关资源
      最近更新 更多