1

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?

Community
  • 1
  • 1
RM.
  • 1,984
  • 19
  • 29
  • The code you posted is the Form1_Load. Why are you getting to the load if the form is already loaded? Or is form loaded? The left picture with the X is the programming running when just the X gets displayed? – jdweng Feb 07 '20 at 04:31
  • Check [this](https://stackoverflow.com/questions/25681886/prevent-autoselect-behavior-of-a-system-window-forms-combobox-c). –  Feb 07 '20 at 04:52
  • https://stackoverflow.com/questions/25681886/prevent-autoselect-behavior-of-a-system-window-forms-combobox-c – M Ghous Sarwar Feb 07 '20 at 05:16
  • 1
    Do you want to try a trick? In the `DropDown` event, write this: `comboBox1.Text = "\u200B" + comboBox1.Text;` – Jimi Feb 07 '20 at 05:23
  • 1
    Maybe also add `comboBox1.SelectionLength = comboBox1.Text.Length;` after, so it feels *real*. – Jimi Feb 07 '20 at 05:34
  • @jdweng - I am just setting up the combobox in form load event – RM. Feb 09 '20 at 05:01

1 Answers1

0

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.

Loathing
  • 5,109
  • 3
  • 24
  • 35