Cecilia
Cecilia

Reputation: 1

how to display the listbox items to a label using VB2008

I am trying to display listbox items in a label. After debugging,I get the error : " make sure that the maximun index on the list is less than the list size"

any comment will be highly appreciate,

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

Upvotes: 0

Views: 5457

Answers (2)

Javed Akram
Javed Akram

Reputation: 15344

You can use For Each for display all selected items of a listbox

    For Each Str As String In xNamesListBox.SelectedItems
        xResultLabel.Text += Str & Environment.NewLine
    Next

Upvotes: 2

Binil
Binil

Reputation: 6583

try this

For i As Int16 = 0 To xNamesListBox.SelectedItems.Count - 1
    xResultLabel.Text += xNamesListBox.SelectedItems.Item(i).ToString() & ControlChars.NewLine
Next

Upvotes: 0

Related Questions