0

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

Edited by yagelnnn1

3
Contributors
3
Replies
17
Views
4 Years
Discussion Span
Last Post by tinstaafl
1

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

Votes + Comments
0

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
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.