【问题标题】:How to merge selected items in ListBox into one in vb .NET?如何在 vb .NET 中将 ListBox 中的选定项目合并为一个?
【发布时间】:2021-07-20 23:00:25
【问题描述】:

如何将 ListBox 中的选定项目合并为一个?

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

item1
item2
item3
item4
item5
item6

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

item1
item2
item3 & item5
item4
item6

我该怎么做?

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    这是一种方法:

    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) 
    

    【讨论】:

    • 哇,这正是我想要的。这段代码很棒。这条鳕鱼就像一个魅力。感谢一百万亲爱的 Yair Maron 的帮助。
    • 很高兴,很高兴为您提供帮助
    猜你喜欢
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    相关资源
    最近更新 更多