如何将 ListBox 中的选定项合并为 vb .NET 中的一项?

How to merge selected items in ListBox into one in vb .NET?

如何将 ListBox 中 select 项合并为一个?

例如,我有一个包含 6 个项目的列表框:

item1
item2
item3
item4
item5
item6

并且我想选择任何我喜欢的项目并将它们组合起来,例如我喜欢 select item3 和 item5 以及结果:

item1
item2
item3 & item5
item4
item6

我该怎么做?

方法如下:

Dim selectedItems As New List(Of String)
For Each item As String In Listbox1.SelectedItems 
    selectedItems.Add(item)
Next

Dim newItems As New List(Of String)
Dim unifiedItem As Nullable(Of Integer) = Nothing

For Each item As String In Listbox1.Items
    If Not selectedItems.Contains(item) Then
        newItems.Add(item)
    Else
        If unifiedItem Is Nothing Then
            unifiedItem = newItems.Count
            newItems.Add(item)
        Else
            newItems(unifiedItem) &= " & " & item
        End If
    End If
Next

ListBox1.Items.Clear()
ListBox1.Items.AddRange(newItems.ToArray)