Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - How to override actions for "Up arrow" and "Down arrow" for a textbox?

Tags:

c#

wpf

I have a textbox and below it i have a listbox.

While the user is typing in the textbox if he presses the up or down arrow he should make a selection in the listbox. The textbox detects all the characters (except space) but it seems that it can't detect the arrow presses.

Any solution for this? This is a WPF project btw.

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
        }
    }
like image 209
Kitze Avatar asked Nov 03 '22 23:11

Kitze


1 Answers

I just tried this and it works. Add a preview key down event to the textbox

   private void TextBox_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        if (e.IsDown && e.Key == Key.Down)
            MessageBox.Show("It works");
    }
like image 118
Anthony Russell Avatar answered Nov 12 '22 10:11

Anthony Russell