2011-01-11 40 views
0

我试图在标签中显示列表框项目。 调试后,我得到的错误:“确保名单上的maximun指数小于列表的大小”如何使用VB2008将列表框项目显示为标签

任何评论将是高度赞赏,

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

试试这个

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

它工作完美,只使用整数作为数据类型...非常感谢。 – Cecilia 2011-01-11 15:12:56

2

您可以使用对于每个显示一个列表框的所有选定的项目

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