在不覆盖的情况下处理winform文本框上的箭头键事件 [英] Handling arrow key events on a winform textbox without overriding

查看:185
本文介绍了在不覆盖的情况下处理winform文本框上的箭头键事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,我提供了一个WinForms TextBox实例,希望将自动完成功能附加到该实例.

I have a situation where I'm provided with a WinForms TextBox instance which I want to attach autocomplete functionality to.

我已经弄清楚了自动完成功能(字符串匹配+下拉列表),到目前为止,它的工作情况可靠.

I've got the autocomplete (string matching + dropdown) all figured out and it works reliable so far.

使用键盘导航下拉菜单有什么能力(这种UI的规范也是这样).

What is the ability to navigate the dropdown with the keyboard (as is the norm with this sort of UI).

自然的解决方案是处理文本框的KeyDown(或类似事件)并相应地在下拉列表中移动选择.

The natural solution would be to handle KeyDown (or somesuch) event for the textbox and moving the selection in the dropdown accordingly.

但是,碰巧要做到这一点,您需要重写IsInputKey()事件以允许捕获箭头键事件.替代方法是覆盖ProcessCmdKey()并在那里处理事件.两者的问题在于,由于无法替换文本框实例,因此无法覆盖任何内容.

However, it happens that to do this, you need to override the IsInputKey() event to allow capture of arrow key events. The alternative is to override ProcessCmdKey() and handle the event there. The problem with these two is that I cannot override anything since I can't replace the textbox instance.

编辑:假设我有以下代码:

Let's assume I have the code below:

void _textBox_KeyDown(object sender, KeyEventArgs e)
{
    if (_dropdown.Visible)
    {
        // TODO The stuff below fails because we need to either handle ProcessCmdKey or override IsInputKey
        switch (e.KeyCode)
        {
            case Keys.Tab:
                {
                    // click selected item
                    _dropdown.Items[GetSelectedItemIndex()].PerformClick();
                    break;
                }
            case Keys.Down:
                {
                    // select next (or first) item
                    int i = GetSelectedItemIndex() + 1;
                    if (i >= _dropdown.Items.Count) i = 0;
                    _dropdown.Items[i].Select();
                    break;
                }
            case Keys.Up:
                {
                    // select previous (or last) item
                    int i = GetSelectedItemIndex() - 1;
                    if (i < 0) i = _dropdown.Items.Count - 1;
                    _dropdown.Items[i].Select();
                    break;
                }
        }
    }
}

上面的代码存在的问题是从未调用.该事件永远不会触发箭头键.更多信息:上,下,左和向右箭头键不会触发KeyDown事件

Them problem with the code above is that it is never called. The event is never triggered for arrow keys. More info: Up, Down, Left and Right arrow keys do not trigger KeyDown event

推荐答案

我可能无法完全理解您的问题,但是这样的方法行不通吗?

I may not be understanding your question entirely, but wouldn't an approach like this work?

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    comboBox1.Text = //results of your matching algorithm.
}

private void textBox1_Validated(object sender, EventArgs e)
{
    textBox1.Text = (string) comboBox1.Text;
}

这篇关于在不覆盖的情况下处理winform文本框上的箭头键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆