Stop changing text when combobox is dropped down

给你一囗甜甜゛ 提交于 2020-02-29 06:31:07

问题


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

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?


回答1:


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.



来源:https://stackoverflow.com/questions/60107053/stop-changing-text-when-combobox-is-dropped-down

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!