DareDevil DareDevil - 4 months ago 17
Vb.net Question

Select List Item using Arrow keys in a UserControl while the TextBox in UserControl has focus

I have a designed a UserControl for List, which further having a UserControl which is ListItem, So far I have gained access by clicking the ListItem on ClickEvent and Up-till Form where I used That ListControl,

The Issue is, I am showing That List Control On a SearchTextBox , I want to do is , When I press Down-Arrow key from Keyboard, I focus to the ListControl, Now I want to control the Up-Down arrow key to select an Item from List,

All the ListItems are add over a Panel Control in UserControl.
enter image description here

Answer

You can override ProcessCmdKey and do what you want.

Assuming you have for example a ListBox in that UserControl:

VB Code

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# Code:

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);
}
Comments