Winforms 组合框错误 - 2 个值相同但键不同的项目 [英] Winforms combobox bug - 2 items with the same value but different key

查看:27
本文介绍了Winforms 组合框错误 - 2 个值相同但键不同的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这真的是 2015 年 Winforms 中的错误还是我做错了什么...

Is this truly a bug in Winforms in 2015 or am I just doing something wrong...

1) 创建一个新的 winforms 项目 (.net 4.0) 并在主窗体中添加一个组合框.2) 将此用于您的表单加载代码:

1) Create a new winforms project (.net 4.0) and add a combobox to the main form. 2) Use this for your form load code:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

    Dim items As New Dictionary(Of Integer, String)

    items.Add(1, "Value 1")
    items.Add(2, "Value 2")
    items.Add(3, "Value 3")
    items.Add(4, "Value 3")

    Dim dataSource As New BindingSource(items, Nothing)
    ComboBox1.DataSource = dataSource

    ComboBox1.DisplayMember = "Value"
    ComboBox1.ValueMember = "Key"

End Sub

注意项目 3 &4 具有相同的值,但不同的键,并且显示和值成员设置正确(除非我发疯了,这是可能的).运行应用程序时,打开组合框并选择最后一项.现在,打开组合框备份,您会注意到现在选择了倒数第二个项目.这是个问题.

Notice how items 3 & 4 have the same value, but different keys and that the display and value members are set correctly (unless I am going crazy, which is possible). When you run the application, open the combobox and select the last item. Now, open the combobox back up and you will notice that the 2nd to last item is now selected. That is a problem.

有什么想法吗?

谢谢!

我在表单中添加了一个 Infragistics UltraComboEditor 并在表单加载事件中放置了以下代码:

I added an Infragistics UltraComboEditor to the form and placed the following code in the form load event:

    For Each item As KeyValuePair(Of Integer, String) In items
        UltraComboEditor1.Items.Add(New ValueListItem With {.DataValue = item.Key, .DisplayText = item.Value})
    Next

    UltraComboEditor1.SelectedIndex = 0
    UltraComboEditor1.AutoComplete = True

Infragistics 控件允许我自动完成并输入我自己的文本,当我选择一个与上面的项目具有相同文本的项目时,它不会改变我的选择.Winforms 控件不应该像那样改变我的选择.

The Infragistics control allows me to autocomplete and enter my own text and it is not changing my selection when I select an item with the same text as the item above it. The Winforms control should not be changing my selection like that.

推荐答案

ComboBox 允许编辑文本部分时,它将模式匹配并突出显示匹配的第一个前缀文本.这具有副作用,即当列表框关闭时,所选项目会更新.

When the ComboBox allows the text portion to be edited, then it will pattern match and highlight the first prefix text that matches. This has the side effect that when the listbox is closed, the selected item is updated.

ComboBox's DropDownStyle == DropDownList 模式时,之前选中的项目会在下拉列表中高亮显示.

When the ComboBox's DropDownStyle == DropDownList mode, then the item previously selected will be highlighted in the dropdown list.

您可以通过将 NativeWindow 分配给 list 窗口然后监听 LB_SETCURSEL Msg 来更改行为>.

You can change the behavior by assigning a NativeWindow to the list window and then listen for the LB_SETCURSEL Msg.

您可以使用此线程作为起点:防止 System.Window.Forms.ComboBox (C#) 的自动选择行为

You can use this thread as a starting point: Prevent AutoSelect behavior of a System.Window.Forms.ComboBox (C#)

向 Data 对象添加 int index 字段.然后在 Register 方法中添加:

Add an int index field to the Data object. Then in the Register method add:

    combo.SelectedIndexChanged += delegate {
        data.index = combo.SelectedIndex;
    };

然后将 Data 传递给本机窗口,该窗口会跟踪先前选择的索引.

Then pass the Data to the native window, which keeps track of the previously selected index.

private class NW : NativeWindow {
    Data data;
    public NW(IntPtr handle, Data data) {
        this.AssignHandle(handle);
        this.data = data;
    }

    private const int LB_FINDSTRING = 0x018F;
    private const int LB_FINDSTRINGEXACT = 0x01A2;
    private const int LB_SETCURSEL = 0x0186;

    protected override void WndProc(ref Message m) {
        if (m.Msg == LB_FINDSTRING)
            m.Msg = LB_FINDSTRINGEXACT;

        if (m.Msg == LB_SETCURSEL)
            m.WParam = (IntPtr) data.index;

        base.WndProc(ref m);
    }
}

这篇关于Winforms 组合框错误 - 2 个值相同但键不同的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆