在 UserControl 中的 TextBox 具有焦点时将 UserControl 中的箭头键处理为 select List Item

Handle Arrow keys in a UserControl to select List Item while the TextBox in UserControl has focus

我为 List 设计了一个 UserControl,它还有一个 UserControl,即 ListItem,到目前为止,我已经获得了访问权限单击 ClickEvent 上的 ListItem 和我使用的 Up-till Form That ListControl,

问题是,我在 SearchTextBox 上显示列表控件,我想做的是,当我按下键盘上的向下箭头键时,我会聚焦到 ListControl ,现在我想控制 Up-Down 方向键 select List 中的一个 Item,

所有 ListItems 都添加到 UserControl 中的面板控件上。

您可以覆盖 ProcessCmdKey 并执行您想要的操作。

假设你有一个 ListBox 在那个 UserControl:

VB代码

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
    If keyData = Keys.Down Then
        'Perform validations and so on then
        Me.ListBox1.SelectedIndex = Me.ListBox1.SelectedIndex + 1
        Return True
    End If
    Return MyBase.ProcessCmdKey(msg, keyData)
End Function

C#代码:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == Keys.Down)
    {
        //Perform validations and so on then
        this.listBox1.SelectedIndex= this.listBox1.SelectedIndex+1;
        return true;
    }
    return base.ProcessCmdKey(ref msg, keyData);
}