Khaled Salah
Khaled Salah

Reputation: 27

add similar first number of listbox to another listbox vb.net

i have items in listbox like this

0,11,41,50
1,5,66,75
1,10,40,50
2,3,43,50
2,7,63,75
2,11,46,50

i need to add similar start number to 1 item like this

0,11,41,50
1,5,66,75 * 1,10,40,50
2,3,43,50 * 2,7,63,75 * 2,11,46,50

Upvotes: 1

Views: 48

Answers (1)

Idle_Mind
Idle_Mind

Reputation: 39142

String.Split() your sets on "," and use the first item as the KEY. Add each set to a Dictionary(Of String, List(Of String)) using that key. Then iterate over the Dictionary and combine the sets using String.Join():

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim buckets As New Dictionary(Of String, List(Of String))
    For Each items In ListBox1.Items
        Dim key As String = items.split(",")(0)
        If Not buckets.ContainsKey(key) Then
            buckets.Add(key, New List(Of String))
        End If
        buckets(key).Add(items)
    Next

    ListBox2.Items.Clear()
    For Each kvp As KeyValuePair(Of String, List(Of String)) In buckets
        ListBox2.Items.Add(String.Join(" * ", kvp.Value))
    Next
End Sub

Output:

enter image description here

Upvotes: 1

Related Questions