我有一个显示通讯组成员的listBox。我有多列选择的列表框设置,以选择多个成员。我试图做一个循环来获取所选项目的文本(索引' s)。这是我的代码,适用于第一项。在第二个项目上,它崩溃并说"索引超出了数组的范围。"我将把这些成员带到会议请求的To:字段中。所以我需要格式化如下:member1; member2。我只是使用MsgBox来测试我回来的内容。但我猜我需要添加一个数组。请帮忙!
For i = 0 To (myListBox.Items.Count - 1)
If myListBox.GetSelected(i) Then
MsgBox(myListBox.SelectedItems(i))
End If
Next
答案 0 :(得分:2)
重新阅读该问题,我认为您希望将其存储为字符串。这应该做你想要的:
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
答案 1 :(得分:0)
这就是我想要的方式。
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