Mal*_*ist 5 .net c# combobox autocomplete winforms
如何允许我的程序用户键入一个值并让它自动完成,但是,我还要阻止他们输入新数据,因为这会导致数据不可用(除非您可以直接访问数据库).
有谁知道如何做到这一点?
不使用下拉式组合框的原因是因为输入数据是输入数据然后拒绝不属于列表中选项的字符是因为它对用户来说更容易.
如果您使用过Quickbook的计时器,那就是我想要的组合框样式.
感谢BFree的帮助,但这是我正在寻找的解决方案.ComboBox使用DataSet作为源代码,因此它不是自定义源.
protected virtual void comboBoxAutoComplete_KeyPress(object sender, KeyPressEventArgs e) {
if (Char.IsControl(e.KeyChar)) {
//let it go if it's a control char such as escape, tab, backspace, enter...
return;
}
ComboBox box = ((ComboBox)sender);
//must get the selected portion only. Otherwise, we append the e.KeyChar to the AutoSuggested value (i.e. we'd never get anywhere)
string nonSelected = box.Text.Substring(0, box.Text.Length - box.SelectionLength);
string text = nonSelected + e.KeyChar;
bool matched = false;
for (int i = 0; i < box.Items.Count; i++) {
if (((DataRowView)box.Items[i])[box.DisplayMember].ToString().StartsWith(text, true, null)) {
matched = true;
break;
}
}
//toggle the matched bool because if we set handled to true, it precent's input, and we don't want to prevent
//input if it's matched.
e.Handled = !matched;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5409 次 |
| 最近记录: |