简体   繁体   English

WinForms列表框keydown事件不会触发

[英]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. 我正在尝试制作一个WinForm ListBox,您可以在其中使用箭头键来循环通过。 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 问题是ListBox的keyDown事件永远不会触发

    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. 我已经把MainForm的keypreview属性正确了。

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. 编辑我在Microsoft文档中看到,我需要重写ProcessDialogKey方法,但是我不确定自己需要做什么。

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 覆盖ProcessDialogKey

Is there already a built-in way to enable this behaviour? 是否已经有内置的方法来启用此行为?

What did I miss? 我错过了什么?

Thanks! 谢谢!

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. 通过查看Designer.cs文件中的代码,看起来您实际上并没有将剪贴板历史列表控件连接到剪贴板历史列表_关键事件处理程序中。 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: 您可以通过Visual Studio表单设计器中“属性”窗口的“事件”子选项卡来完成此操作(查找小小的闪电图标),然后通过设计器将事件连接起来,或者,也可以在代码中进行操作:

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

    clipboardHistoryList.KeyDown += clipboardHistoryList_KeyDown;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM