1

我正在尝试查看列表框是否为空。我已经看到了使用的建议

If IsNull(txtLevel) Then
    MsgBox "No Item is Selected"

但即使选择了项目,这也会返回错误 MsgBox。

我在使用其他代码时遇到的另一个问题,If txtLevel.ListIndex = "-1"或者If txtLevel.listount = 0它第一次运行良好,但是如果您选择然后取消选择,这不会触发错误消息。

编辑:对我来说有效的答案是:If txtLevel.ItemsSelected.Count = 0

4

3 回答 3

1

您还可以使用.ItemsSelected返回变量数组的属性,该数组包含所选条目的行号,或者在选择参数中指定的行时.Selected返回的属性。True

于 2013-09-18T23:40:36.640 回答
1

有效的答案是“如果 txtLevel.ItemsSelected.Count = 0”

于 2013-09-19T16:53:27.537 回答
0

试试这个代码 - 看看你是否可以在其中看到你的解决方案。我留下评论来解释发生了什么。

  • ComboData 是一个组合框控件
  • CheckNoComboData 是一个复选框控件
  • CheckSelection 是一个复选框控件
  • CheckNoSelection 是一个复选框控件

代码:

  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
于 2013-09-19T15:43:55.327 回答