我正在尝试在标签中显示列表框项目。 调试后,我收到错误:“确保列表中的最大索引小于列表大小”
任何评论都会非常感激,
Private Sub xMultiButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles xMultiButton.Click
Dim count As Integer
count = Me.xNamesListBox.Items.Count
For count = 0 To 3
Me.xResultLabel.Text = Me.xNamesListBox.SelectedItems.Item(0).ToString & ControlChars.NewLine _
& Me.xNamesListBox.SelectedItems.Item(1).ToString & ControlChars.NewLine _
& Me.xNamesListBox.SelectedItems.Item(2).ToString & ControlChars.NewLine _
& Me.xNamesListBox.SelectedItems.Item(3).ToString & ControlChars.NewLine _
& Me.xNamesListBox.SelectedItems.Item(4).ToString
Next
End Sub
答案 0 :(得分:2)
您可以使用 For Each 显示列表框中的所有选定项目
For Each Str As String In xNamesListBox.SelectedItems
xResultLabel.Text += Str & Environment.NewLine
Next
答案 1 :(得分:0)
试试这个
For i As Int16 = 0 To xNamesListBox.SelectedItems.Count - 1
xResultLabel.Text += xNamesListBox.SelectedItems.Item(i).ToString() & ControlChars.NewLine
Next