我正在尝试查看列表框是否为空。我已经看到了使用
的建议If IsNull(txtLevel) Then
MsgBox "No Item is Selected"
但即使选择了项目,也会返回错误MsgBox。
我在使用其他代码If txtLevel.ListIndex = "-1"
或If txtLevel.listount = 0
时遇到的另一个问题是它第一次运行良好,但如果您选择然后取消选择,则不会触发错误消息。
编辑:我的答案有效:If txtLevel.ItemsSelected.Count = 0
答案 0 :(得分:1)
您还可以使用.ItemsSelected
属性,该属性返回包含所选条目的行号的变量数组,或者在指定行时返回.Selected
的{{1}}属性在参数中被选中。
答案 1 :(得分:0)
尝试此代码 - 看看您是否可以在此处看到您的解决方案。我留下评论来解释发生了什么。
代码:
Dim intIter As Integer
Dim boolItems As Boolean
' Check if there is no Row Source data
If Nz(Me.ComboData.RowSource, "") = "" Then
Me.CheckNoComboData = True
Else
Me.CheckNoComboData = False
End If
' Check if there is a row source, but no
' items resulting from that rowsource
If Me.ComboData.ListCount = 0 Then
Me.CheckNoComboData = True
Else
Me.CheckNoComboData = False
End If
' Check if any items in the listbox are selected or not
Items = False
' Loop through each item in the combobox
For intIter = 0 To (Me.ComboData.ListCount - 1)
' If its selected, then we know items are selected
If Me.ComboData.Selected(intIter) Then
Items = True
Exit For
End If
Next
' Return Results
Me.CheckSelection = Items
Me.CheckNoSelection = Not Items
答案 2 :(得分:0)
有效的答案是“如果txtLevel.ItemsSelected.Count = 0”