Search code examples
c#winformscombobox

Stop changing text when combobox is dropped down


Using WinForms. I have a combobox with DropDownStyle DropDown. In the items I put a single item "XA". When the user enters "X" into the ComboBox (not yet dropped down) and then presses the drop down button "X" is automatically replaced with "XA". How can I stop this happening? I would like the user to be able to keep text as "X" and only change the text to "XA" if "XA" was clicked in the drop down list. To recreate create a new WinForms application and add a comboBox then add the following code

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
            comboBox1.Items.Add("XA");
        }

enter image description here enter image description here

Note that if the user does not press the dropdown then "X" stays in the combobox.

Note that there is a similar sounding question here but it is actually different. How do I set a ComboBox default *not in the drop down* when a list item begins with the same text as a drop down item?


Solution

  • I think this solution should help you:

    Winforms combobox bug - 2 items with the same value but different key

    It changes if (m.Msg == LB_FINDSTRING) to m.Msg = LB_FINDSTRINGEXACT;, which should prevent the behavior you describe.