【问题标题】:Get text of listBox.selectedItems?获取 listBox.selectedItems 的文本?
【发布时间】:2015-01-27 17:22:17
【问题描述】:

我有一个显示通讯组成员的列表框。我有用于多重简单选择的列表框设置来选择多个成员。我正在尝试循环获取所选项目(索引)的文本。这是我的代码,它适用于第一项。在第二个项目上它崩溃并说“索引超出了数组的范围”。我将把这些成员放在会议请求的收件人:字段中。所以我需要像这样格式化:member1;member2。我只是在使用 MsgBox 来测试我得到了什么。但我猜我需要添加到数组中。请帮忙!

For i = 0 To (myListBox.Items.Count - 1)
        If myListBox.GetSelected(i) Then
            MsgBox(myListBox.SelectedItems(i))
        End If
    Next

【问题讨论】:

  • 可能是 SelectedItems 中的 For Each 项目?
  • 我如何将它添加到上面提到的数组中?

标签: vba for-loop listbox


【解决方案1】:

重新阅读问题,我认为您想将其存储为字符串。这应该可以满足您的需求:

Documentation on ListBox.Selected()

Dim MailStr as String

MailStr = ""

If myListBox.SelectedItems.Count = 0 Then
    MsgBox "No User Selected"
    Exit Sub
End If
For i = 0 to (myListBox.Items.Count - 1)
    If myListBox.Selected(i) Then
        MailStr = MailStr & myListBox.Items.Item(i) & "; "
    End If
Next i

你也可以试试:

Dim assets As String 

 assets = ""

 For Each item In assetListBox.SelectedItems
        If assets = Nothing Then
            assets = item
            'Outputs 1 item "Member1"
            MsgBox(assets)
        Else
            assets = assets & "; " & item
            'Outputs 2 items in format "member1; member2"
            MsgBox(assets)
        End If
    Next

【讨论】:

    【解决方案2】:

    这就是我想要的方式。

    Dim assets As String 
    
     assets = ""
    
     For Each item In assetListBox.SelectedItems
            If assets = Nothing Then
                assets = item
                'Outputs 1 item "Member1"
                MsgBox(assets)
            Else
                assets = assets & "; " & item
                'Outputs 2 items in format "member1; member2"
                MsgBox(assets)
            End If
        Next
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-04
      • 2010-09-29
      • 2013-03-31
      • 2010-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多