Search code examples
c#winformslistboxkeydowneventtrigger

WinForms listbox keydown event won't fire


I am trying to make a WinForm ListBox in which you can loop trough using the arrow keys. I also have two buttons on which you can click to go up and down the list. The buttons do produce the desired effect. The problem is that the ListBox's keyDown event is never triggered

    public MainForm()
    {
        InitializeComponent();
        if (this.clipboardHistoryList.Items.Count > 0)
            this.clipboardHistoryList.SetSelected(0, true);
        clipboardHistoryList.Select();
    }

   private void goUpButton_Click(object sender, EventArgs e)
    {
        goUpList();
    }

    private void goDownButton_Click(object sender, EventArgs e)
    {
        goDownList();
    }

    private void goDownList()
    {
        if (clipboardHistoryList.SelectedIndex == clipboardHistoryList.Items.Count - 1)
        {
            clipboardHistoryList.SetSelected(0, true);
        }
        else
        {
            clipboardHistoryList.SetSelected(clipboardHistoryList.SelectedIndex + 1, true);
        }
    }

    private void goUpList()
    {
        if (clipboardHistoryList.SelectedIndex == 0)
        {
            clipboardHistoryList.SetSelected(clipboardHistoryList.Items.Count - 1, true);
        }
        else
        {
            int l_currentlySelected = clipboardHistoryList.SelectedIndex;
            clipboardHistoryList.SetSelected(l_currentlySelected - 1, true);
        }
    }

    private void clipboardHistoryList_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Up)         //Brekpoint is never reached
        {
            goUpList();
        }
        else if (e.KeyCode == Keys.Down)
        {
            goDownList();
        }
    }

I have put the MainForm's keypreview proprety to true.

The arrow keys do work by default on a listbox but they won't let you go from last to first element if you press the down arrow on the last element --hopes this makes sense.

EDIT I have seen on Microsoft's documentation that I need to override the ProcessDialogKey method but I am not exactly sure of what I need to do.

Perform special input or navigation handling on a control. For example, you want the use of arrow keys in your list control to change the selected item. Override ProcessDialogKey

Is there already a built-in way to enable this behaviour?

What did I miss?

Thanks!


Solution

  • From looking at the code in your Designer.cs file, it doesn't look like you've actually got your clipboardHistoryList control wired into your clipboardHistoryList_KeyDown event handler. You can do that through the "Events" subtab of the Properties window in your visual studio form designer (look for the little lightning bolt icon) and wire up the event through the designer that way, or alternatively you can do it in code:

    public MainForm()
    {
        InitializeComponent();
        if (this.clipboardHistoryList.Items.Count > 0)
            this.clipboardHistoryList.SetSelected(0, true);
        clipboardHistoryList.Select();
    
        clipboardHistoryList.KeyDown += clipboardHistoryList_KeyDown;
    }