Key Up/Down 在文本框 C# 中选择自动完成列表

Key Up/Down Selection of Autocomplete list in Textbox C#

在 C# Winforms 中,我有一个带有自动完成模式的文本框。当用户键入一些字母时,建议列表会正确填充。但是,如果使用(键盘)向上和向下键选择列表中的项目,则无法浏览项目列表。它只会选取列表中显示的第一项。

另一方面,使用鼠标点击选择效果很好。这是我的代码

    private void txtQryName_TextChanged(object sender, EventArgs e)
    {
        List<string> fullName = _customerBll.NameSuggestor(txtQryName.Text);
        AutoCompleteStringCollection source = new AutoCompleteStringCollection();
        source.AddRange(fullName.ToArray());
        txtQryName.AutoCompleteMode = AutoCompleteMode.Suggest;
        txtQryName.AutoCompleteSource = AutoCompleteSource.CustomSource;
        txtQryName.AutoCompleteCustomSource = source;
    }

基于键的选择的问题是它对堆栈包含的项目数非常敏感。一种可能的解决方案是将 KEY_UP 和 KEY_DOWN 设置为仅 increase/decreases 或在列表中选择特定类型。

此外,我相信您可以将文本框设置为在您按下它时有特定的响应,您应该考虑到,如果例如用户没有在(布尔值:false 或 true)中选择一个项目,那么没有选择任何项目

一个例子

private void textBox1_KeyPress
(object sender,System.Windows.Forms.KeyPressEventArgs e)
{
    // Check for the flag being set in the KeyDown event.
    if (nonNumberEntered == true)
    {
        e.Handled = true;
    }
}

https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown(v=vs.110).aspx

如果你想扩展这个想法,这里有一些对你有用的东西。希望我的回答有帮助