【发布时间】:2013-03-05 08:38:17
【问题描述】:
我的朋友们,我的 windows 窗体中有一个组合框,我可以用数据库中的数据填充它, 但是当用户在组合框内输入字母时,我无法填充组合框, 例如,当用户在组合框旁边键入字母“R”时,组合框必须下拉并显示所有可能的字母“R”
【问题讨论】:
-
参考this一个
我的朋友们,我的 windows 窗体中有一个组合框,我可以用数据库中的数据填充它, 但是当用户在组合框内输入字母时,我无法填充组合框, 例如,当用户在组合框旁边键入字母“R”时,组合框必须下拉并显示所有可能的字母“R”
【问题讨论】:
yourComboBox.AutoCompleteSource 设置为AutoCompleteSource.ListItems;(如果您的yourComboBox.Items 已从数据库中填充)yourComboBox.AutoCompleteMode 设置为SuggestAppend
【讨论】:
您必须与组合框上的 KeyUp 事件相关联,并使用 comboBox.Text 过滤 comboBox.Items 集合以仅显示包含键入的字符。您还需要强制组合框窗口下拉。
【讨论】:
希望对您有所帮助:
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
【讨论】: