使用WinForms。我有一个带有DropDownStyle DropDown的组合框。在项目中,我放置了一个项目“ XA”。当用户在组合框中输入“ X”(尚未下拉),然后按下下拉按钮时,“ X”将自动替换为“ XA”。我该如何阻止这种情况的发生?我希望用户能够将文本保留为“ X”,并且仅在下拉列表中单击“ XA”时才将文本更改为“ XA”。要重新创建,请创建一个新的WinForms应用程序并添加一个comboBox,然后添加以下代码
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
comboBox1.Items.Add("XA");
}
请注意,如果用户未按下拉菜单,则“ X”将留在组合框中。
请注意,这里有一个相似的问题,但实际上是不同的。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?
我认为此解决方案应为您提供帮助:
Winforms combobox bug - 2 items with the same value but different key
将if (m.Msg == LB_FINDSTRING)
更改为m.Msg = LB_FINDSTRINGEXACT;
,这应防止您描述的行为。