Search code examples
c#.netwinformscomboboxwindows

Enter Key Issue In ComboBox with AutoCompleteMode set to Append


Pressing Enter key clears text of ComboBox when drop down is open in a ComboBox with AutoCompleteMode set to Append.

We know in widows forms when the AutocompleteMode property in the ComboBox is set to Append then we get the values before we type the complete text of an item.

Problem is here:

  • I click the dropdown button and open the drop down
  • I try to enter some characters, I get the values as expected and it completes the text.
  • But when I press Enter it deletes the text of combo box.

How can I have Append option and also make pressing Enter when the Dropdown is open, keep entered text and don't remove it.


I tried the None option in the "Auto complete Mode" property it is working fine but there is no append of data....

I dont need the suggest and suggest append options in the "Auto complete Mode" property since it opens another dropdown window....

I need to type data while the data in the dropdown box is listed and when i get the append values just by clicking enter button it should work(without getting deleted)...

Is this possible?

Thanks


Solution

  • When the dropdown is closed, it works as expected, but when the dropdown is open, pressing Enter closes the dropdown and removes entered text.

    As a solution you can derive from ComboBox and override IsInputKey this way:

    public class MyComboBox : ComboBox
    {
        protected override bool IsInputKey(Keys keyData)
        {
            switch ((keyData & (Keys.Alt | Keys.KeyCode)))
            {
                case Keys.Enter:
                case Keys.Escape:
                    if (this.DroppedDown)
                    {
                        this.DroppedDown = false;
                        return false;
                    }
                    break;
            }
            return base.IsInputKey(keyData);
        }
    }