Handling arrow key events on a winform textbox without overriding

喜夏-厌秋 提交于 2019-12-10 07:42:02

问题


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.

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

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

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.

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

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


回答1:


I hope i haven't missunderstood you, but is this a solution:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Down)
    {
         // Place logic for textbox here
    }
}

I'd use a KeyDown event on the form and then compare the keycode with the Keys.Down keycode

Not working

see here: Up, Down, Left and Right arrow keys do not trigger KeyDown event




回答2:


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


来源:https://stackoverflow.com/questions/28110999/handling-arrow-key-events-on-a-winform-textbox-without-overriding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!