繁体   English   中英

如何在用户在c#中的组合框内键入拼写时创建组合框自动填充

[英]How to Create combo box to auto fill while user type the spelings inside the combo Box in c#

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

  1. yourComboBox.AutoCompleteSource设置为AutoCompleteSource.ListItems; (如果你的yourComboBox.Items已经从数据库填充)
  2. yourComboBox.AutoCompleteMode设置为SuggestAppend

您必须使用comboBox上的KeyUp事件,并使用comboBox.Text过滤comboBox.Items集合以仅显示包含的类型字符。 您还需要强制comboBox窗口下拉。

希望这可以帮助你:

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

这里开始

暂无
暂无

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

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