简体   繁体   English

有时ActiveX组合框仅显示一行,为什么?

[英]Sometimes the ActiveX Combobox only shows one row, why?

It seems that when I first click on the combobox and then click on the arrow, all items are displayed. 看来,当我第一次单击组合框,然后单击箭头时,将显示所有项目。

While if I click on the arrow without clicking on the combobox before, only one item is displayed and I can click on scroll buttons to see the other items. 如果我单击箭头而未单击组合框,则仅显示一个项目,并且我可以单击滚动按钮以查看其他项目。

Why does this happen? 为什么会这样?

Here is the macro I am using to populate the combobox with items 这是我用来在组合框中填充项目的宏

Private Sub ComboBox1_GotFocus()
    Dim c As Range
    Dim selText As String
    selText = ComboBox1.selText
    ComboBox1.Clear
    For Each c In wConfig.Range("BudgetDropdown").Cells
        ComboBox1.AddItem c.Value
    Next c
    ComboBox1.selText = selText
End Sub

在此处输入图片说明

在此处输入图片说明

To automatically populate combobox with data from named range, set it's ListFillRange property to the name of the range. 要使用命名范围内的数据自动填充组合框,请将其ListFillRange属性设置为范围名称。

You can do it at runtime: 您可以在运行时执行此操作:

ComboBox1.ListFillRange = "BudgetDropdown"

Or by setting it to BudgetDropdown in properties window. 或者在属性窗口BudgetDropdown其设置为BudgetDropdown

Don't know exactly why, but basically when you open a just-cleared-combobox it shows up only 1 lane because it "thinks" it is empty even if you have just refilled it. 不知道确切的原因,但是基本上,当您打开一个刚刚清除的组合框时,它仅显示1条车道,因为它“认为”它是空的,即使您刚刚填充了它。 To trick it, you have to remove all the rows 1 by 1 till you reach your ListCount value (the number of rows your combobox will display). 若要欺骗它,必须逐行删除所有行,直到达到ListCount值(组合框将显示的行数)。 Then you can add all the new rows and after that you can delete the previously omitted rows. 然后,您可以添加所有新行,然后可以删除之前省略的行。 In my case i could not use the ListFillRange because my list was "embended" in several cells; 在我的情况下,我无法使用ListFillRange,因为我的列表在多个单元格中“被包围”了。 so i had to extract it. 所以我不得不提取它。 Something like this: 像这样:

Private Sub Combobox3_GotFocus()

Dim RngRange01 As Range 'Single cell where my row is "embended"
Dim IntCounter01 As Integer 'A counter
Dim BlnCheck as Boolean 'A boolean to remember me if it's the Combobox was empty before i
'focused it

IntCounter01 = ComboBox3.ListCount 'Set the counter equal to ListCount

'I check if the combobox is already filled or not and modify the BlnCheck
BlnCheck = True
If ComboBox3.ListCount = 0 Then BlnCheck = False

'In this For-Next i remove all the rows till i reach the ListRows
For IntCounter01 = IntCounter01 To ComboBox3.ListRows + 1 Step -1
    ComboBox3.RemoveItem IntCounter01 - 1
Next IntCounter01

'Set the range (it's a named list with titles located in another sheet)
Set RngRange01 = Sheets("MySheet1").Cells(2, Sheets("MySheet1").Range("MyList").Column)


Do Until RngRange01.Value = "" 'Cover the whole list

    'This If is to select the right data to insert (originally it was more complicated
    'so i've cut it for this explanation)
    If RngRange01.Offset(0, 1).Value <> RngRange01.Offset(-1, 1).Value Then
        ComboBox3.AddItem RngRange01.Offset(0, 1).Value
    End If

    Set RngRange01 = RngRange01.Offset(1, 0) 'Next cell of the list
Loop

'Now we can remove the rows of the combobox we didn't remove previously (if the combobox 
'wasn't empty already)
If BlnCheck = True then
    For IntCounter01 = ComboBox3.ListRows To 1 Step -1
        ComboBox3.RemoveItem IntCounter01 - 1
    Next IntCounter01
End If

End Sub

So far it will work only after the really first time you focus the combobox. 到目前为止,只有在您真正第一次关注组合框后,它才能起作用。 Still annoying! 还是很烦! To completely remove (trick) the bug you have to add some lanes to the combobox before you focus it; 要完全消除(欺骗)错误,必须在组合框上添加一些通道,然后再集中精力。 perhaps when you open the workbook like this: 也许当您像这样打开工作簿时:

Private Sub Workbook_Open()

Dim IntCounter01 As Integer 'A counter

IntCounter01 = 1 'Set the counter

'In this For-Next we add a lane till we reach the ListRows
For IntCounter01 = IntCounter01 To Sheets("MySheet2").ComboBox3.ListRows
    Sheets("MySheet2").ComboBox3.AddItem ""
Next IntCounter01

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM