我想只允许在Textbox中键入自动完成值,我尝试为KeyPress事件创建代码但不幸的是,当我有一个巨大的自动完成列表时似乎需要很长时间。 这是我的代码:
private void TBAdd_KeyPress(object sender, KeyPressEventArgs e)
{
bool found = false;
List<string> list = ((TextBox)sender).AutoCompleteCustomSource.Cast<string>().ToList<string>();
int i = 0;
while (!found && i < list.Count)
{
if (list[i].Contains(((TextBox)sender).Text + e.KeyChar))
{
found = true;
}
i++;
}
e.Handled = !found;
}
还有其他优化的方法吗?