VSTO 中的错误 - 工作表中的组合框无缘无故向下滚动

Bug in VSTO - Combobox in worksheet scrolling down with no reason

看看下面的代码。此代码完美运行,在单元格 A1 处放置一个组合框,为您提供项目列表。

Dim sheet As Microsoft.Office.Tools.Excel.Worksheet
Dim sheet1 As Excel.Worksheet = Globals.ThisAddIn.Application.ActiveSheet
sheet = Globals.Factory.GetVstoObject(sheet1)

Dim combo As New Windows.Forms.ComboBox()
'combo.AutoCompleteMode = Windows.Forms.AutoCompleteMode.SuggestAppend
combo.AutoCompleteSource = Windows.Forms.AutoCompleteSource.ListItems
combo.Items.AddRange({"test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8", "test9", "test10", "test11", "test12", "test13", "test14", "test15", "test16", "test17", "test18", "test19", "test20", "test21", "test22", "test23", "test24", "test25", "test26", "test27", "test28", "test29", "test30", "test31", "test32", "test33", "test34"})
Me.sheet.Controls.AddControl(combo, Me.sheet.Range("$A"), "test")

当您取消注释将 AutoCompleteMode 设置为 SuggestAppend 的代码行时会出现此问题。

要查看发生的错误,您需要使项目列表足够大以显示滚动条。然后,单击箭头(打开下拉菜单)和 select 下拉菜单的第一项。再做一次,滚动到最后一个元素,然后单击这个。重复操作。

在执行此操作的第二次迭代时,您应该会看到错误。当您按下鼠标按钮时,列表会自动滚动,从而更改光标下的元素。然后,当您释放鼠标按钮时,您 select 一个不是您想要的元素的项目。

有人知道这个问题的解决方法吗?我正在使用 VS2013,并使用 VB.NET 进行编程(如您所见,呵呵)。

谢谢

问题已解决 "jmierzej" here

解法:

我首先将所有项目添加到一个 Dictionary 对象,然后将组合框绑定到该 Dictionary。在下面的代码中,'prefill' 对象是包含组合框内容的字典对象。

    'define the dictionary which will hold the values
    Dim prefill = New Dictionary(Of String, String)

    'add code here to fill Dictionary with your items.  In my case I used identical values for the key/value pair in the Dictionary

    cbox.DataSource = New BindingSource(prefill, Nothing)
    cbox.DisplayMember = "Value"
    cbox.ValueMember = "Key"