【问题标题】:Key Up/Down Selection of Autocomplete list in Textbox C#文本框 C# 中自动完成列表的键上/下选择
【发布时间】:2016-01-03 13:01:21
【问题描述】:

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

另一方面,使用鼠标单击进行选择可以正常工作。这是我的代码

    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;
    }

【问题讨论】:

    标签: c# list autocomplete textbox key


    【解决方案1】:

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

    此外,我相信您可以将文本框设置为在您按下它时有特定的响应,您应该考虑,例如,如果用户没有选择其中的一个项目(布尔值: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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-05
      • 1970-01-01
      • 2014-08-29
      • 2015-11-17
      • 1970-01-01
      • 1970-01-01
      • 2012-07-21
      • 1970-01-01
      相关资源
      最近更新 更多