6

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

4

3 回答 3

5
  1. 设置yourComboBox.AutoCompleteSourceAutoCompleteSource.ListItems;(如果您yourComboBox.Items已经从数据库中填写)
  2. 设置yourComboBox.AutoCompleteModeSuggestAppend
于 2013-03-05T08:45:34.173 回答
1

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

于 2013-03-05T08:44:50.420 回答
1

希望这可以帮助你:

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

这里

于 2013-03-05T08:47:02.790 回答