C # - How to override actions for "Up Arrow" and "Down Arrow" for a text box?

I have a text box and below it I have a list.

While the user enters a text box, if he presses the up or down arrow, he must make a choice in the list. The text box detects all characters (except space), but it seems that it cannot detect arrows.

Any solution for this? This is a WPF project.

EDIT, here's the working code thanks to T.Kiley:

    private void searchBox_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.IsDown && e.Key == Key.Down)
        {
            e.Handled = true;
            //do your action here

        }
        if (e.IsDown && e.Key == Key.Up)
        {
            e.Handled = true;
            //do another action here
        }
    }
+5
source share
2 answers

I just tried this and it works. Add a preview event down in the text box

   private void TextBox_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        if (e.IsDown && e.Key == Key.Down)
            MessageBox.Show("It works");
    }
+3
source

KeyDown TextBox. , ( , , ).

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Down)
    {
        // Do some code... 
    }
}
-1

All Articles