[英]How to Create combo box to auto fill while user type the spelings inside the combo Box in c#
我的朋友們,我在windows窗體中有一個組合框,我可以用數據庫中的數據填充它,但是當用戶在組合框中鍵入字母時,我無法填充組合框。例如,當用戶鍵入字母“R”時“在組合框和組合框的旁邊必須下降並顯示所有可能的字母”R“
yourComboBox.AutoCompleteSource
設置為AutoCompleteSource.ListItems;
(如果你的yourComboBox.Items
已經從數據庫填充) 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.