【问题标题】:data type of Listbox Index列表框索引的数据类型
【发布时间】:2015-01-28 14:47:01
【问题描述】:

当我搜索有关 Listbox 的信息时,我看到大多数人使用 Variant 作为列表索引号的数据类型。 示例:

Dim varItm As Variant
If Me.myListBox.ItemData.Selected(varItm) = True 

使用 Variant 还是 Integer 作为数据类型更好?

【问题讨论】:

    标签: list ms-access listbox vba


    【解决方案1】:

    你混淆了两件事。当您有一个多选 ListBox 时,用户将使用以下代码,因为 .SelectedItems 集合必须是变体:

    Dim varItm as Variant
    For Each varItm in myListBox.SelectedItems
        'Do Something
    Next varItm
    

    但是,myListBox.Sected(x) 上有 further documentation,其中 x 应该是 Long,但实际上可以是包含变量的任何数字。 Variant 允许存储数字,因此它可以工作,但最好将 x 定义为明确包含数字(IntegerLongMore on Integer vs. Long

    Dim x As Integer
    For x = 0 to myListBox.ListCount - 1
        If myListBox.Selected(x) Then
            'Do Something
        End If
    Next x
    

    【讨论】:

      猜你喜欢
      • 2015-06-07
      • 2019-02-04
      • 2022-06-29
      • 2016-03-28
      • 1970-01-01
      • 2018-04-27
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      相关资源
      最近更新 更多