Search code examples
visual-studiowinformscomboboxado.net

how to select index in a combobox using DOWN ARROW key?


I want this functionalitymy combobox has multiple values. for example: RJ11, RJ12, RJ13 and so on. AutoCompleteMode set to append and AutoCompleteSource set to listItems. all i want is when i type rj in combobox it will suggest me rj11 in combobox's text field and when i press DOWN ARROW key it should select rj11 but what it is doing is it keep searching for next possible match.

i tried :

private void comboBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if(e.KeyCode == Keys.Down)
        {
            comboBox1.SelectedIndex = comboBox1.FindStringExact(comboBox1.Text);
        }
    }

but it is not working. i am very new to winform and programing. please help. and yeah sorry for my bad english.


Solution

  • You can try to refer to the following code to select the index in a combobox by using down key.

    We can override the ProcessCmdkey method in your form.

    Code:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
            {
                if ((this.ActiveControl == comboBox1) && (keyData == Keys.Down))
                {
                    comboBox1.SelectedIndex = comboBox1.FindStringExact(comboBox1.Text);
                    return true;
                }
                else
                {
                    return base.ProcessCmdKey(ref msg, keyData);
                }
            }