Repopulate a combobox itemlist without affecting what is currently written inside the combobox?
如何在不影响当前写入组合框中的内容的情况下重新填充组合框项目列表?
我目前的代码看起来有点像:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Private Sub ComboBox1_DropButtonClick() Dim v As Variant Dim selText As String selText = ComboBox1.selText Dim i As Integer For i = 0 To ComboBox1.ListCount - 1 ComboBox1.RemoveItem (0) Next i v = Call CreateList() For i = 0 to Ubound(v) ComboBox1.AddItem v(i) Next v If selText <>"" Then ComboBox1.Text = selText End Sub |
我以前没有使用过组合框,所以可能有更好的方法。上面的代码有几个问题
- 它更改组合框的选定值(在尝试恢复它之前)
- 它触发 Combobox1_change 事件
- 我认为有些循环可能是不必要的。可以在不循环的情况下删除所有项目并在不循环的情况下添加多个项目。要添加的项目是从具有逗号分隔值的字符串创建的。
非常感谢您的帮助
There are loops that I think might be unnecessary. Is is possible to remove all items without looping and adding multiple items without looping.
1 2 3 4 5 6 7 8 9 | Private Sub Sample() Dim MyAr(1 To 5) For i = 1 To 5 MyAr(i) = i Next i ComboBox1.List = MyAr End Sub |
It triggers the Combobox1_change event
为了防止
这样的布尔变量
It changes the selected value of the combobox (before attempting to restore it)
将值存储在变量中,然后如果该值是下拉列表的一部分,则重置组合框值