Hey, I think it's a simple code but I can't resolve how to do it..

I have a combobox and I want to "Add All" to a listbox, but without removing them from the combobox..

Here is my code, please help me fix it:

        If ListBox1.Items.Contains(ComboBox1.Items) Then
            MsgBox("You can't add those items twice.", MsgBoxStyle.Exclamation)
        Else
            Dim Item As String
            For Each Item In ComboBox1.Items
                With ListBox1.Items
                    .Add(Item)
                End With
            Next
        End If

Recommended Answers

All 3 Replies

I have a combobox and I want to "Add All" to a listbox, but without removing them from the combobox..

One quick and simple way to do that is to use the combobox.items collection as the datasource for the listbox. Something like this: ListBox1.DataSource = ComboBox1.Items

commented: Nice +7

How about reworking your code to say

        For Each Item In ComboBox1.Items
            If ListBox1.Items.Contains(Item) Then
                MsgBox("You can't add those items twice.", MsgBoxStyle.Exclamation)
            Else
                ListBox1.Items.Add(Item)
            End If
        Next

Just a side note. Instead of trying to trap whether the user clicks a button twice. You might find it better to just disable the button(Button1.Enabled = False) once the items are added to the listbox.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.