如何显示带有多扩展名的列表框项目以显示标签中的项目?

时间:2011-04-05 19:33:50

标签: vb.net

我的作业问题是在标签中显示列表框(多重扩展)中的所选项目。我需要在标签中显示所有选定的名称。请你能用最简单,最简洁的方式帮助我。

谢谢!

这是我所拥有的,但它不起作用。

Private Sub MainForm_Load(ByVal sender As Object, ByVal e As
     

System.EventArgs)处理Me.Load           '用值

填充列表框
    namesListBox.Items.Add("Debbie")
    namesListBox.Items.Add("Bill")
    namesListBox.Items.Add("Jim")
    namesListBox.Items.Add("Ahmad")
    namesListBox.Items.Add("Carol")

End Sub

Private Sub multiButton_Click(ByVal sender As
     

System.Object,ByVal e As   System.EventArgs)句柄   multiButton.Click

     

resultLabel.Text =“”

    Dim name As String = namesListBox.SelectedItems.ToString

    resultLabel.Text = name & ControlChars.NewLine

End Sub

2 个答案:

答案 0 :(得分:1)

尝试SelectionChanged事件

Private Sub listBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles listBox2.SelectionChanged
    Dim stringBuilder = New StringBuilder()
    Dim delimiter = " - "
    For Each item As Object In listBox2.SelectedItems

        stringBuilder.AppendFormat("{0}{1}", item, delimiter)
    Next
    label2.Text = stringBuilder.ToString()
End Sub

答案 1 :(得分:1)

ListBox的SelectedItems属性是一个集合。您需要迭代集合。

resultLabel.Text = String.Empty
For Each si As Object In namesListBox.SelectedItems
    resultLabel.Text += si.ToString() & ControlChars.NewLine
Next