如何使用VB2008将列表框项目显示到标签

时间:2011-01-11 04:16:15

标签: vb.net

我正在尝试在标签中显示列表框项目。 调试后,我收到错误:“确保列表中的最大索引小于列表大小”

任何评论都会非常感激,

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

2 个答案:

答案 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