我有同樣的問題,並尋找一個快速解決方案。
最終我最終自己寫了。這有點骯髒,但如果需要,它不應該很難變得更漂亮。
這個想法是在每次按鍵後重新構建組合列表。這樣我們就可以依靠組合的內置界面上,我們並不需要自己實現一個文本框和列表框界面...
只記得設置combo.Tag
到null
如果你重新編譯組合的選項列表。
private void combo_KeyPress(object sender, KeyPressEventArgs e) {
comboKeyPressed();
}
private void combo_TextChanged(object sender, EventArgs e) {
if (combo.Text.Length == 0) comboKeyPressed();
}
private void comboKeyPressed() {
combo.DroppedDown = true;
object[] originalList = (object[])combo.Tag;
if (originalList == null) {
// backup original list
originalList = new object[combo.Items.Count];
combo.Items.CopyTo(originalList, 0);
combo.Tag = originalList;
}
// prepare list of matching items
string s = combo.Text.ToLower();
IEnumerable<object> newList = originalList;
if (s.Length > 0) {
newList = originalList.Where(item => item.ToString().ToLower().Contains(s));
}
// clear list (loop through it, otherwise the cursor would move to the beginning of the textbox...)
while (combo.Items.Count > 0) {
combo.Items.RemoveAt(0);
}
// re-set list
combo.Items.AddRange(newList.ToArray());
}
來源
2012-10-09 00:04:35
obe
如何把它寫在C#中的一個WinForms – ca9163d9 2014-11-14 03:28:50