locked
Adding Items to a Listbox RRS feed

  • Question

  • I have two list boxes.  I add items form one to another by highlighting in the first box, and then by clicking a button.  However, if I do this in two steps a problem occurs.  Say I have three items in listbox one.  If I highlight two of them and add them to the second box then go back and highlight the third item and then try to add this to the box with the two items, the two previous items disappear.  Likely has to do with the .AddRange Method in the If stmt below.  I need a test which checks to see if the values in item() are already in the second list box.  how do I do this?


            For Each item As Object In ListBox1.SelectedItems
                items(i) = item
                i = i + 1
            Next

            If Me.ListBox1.SelectedItem IsNot Nothing Then
                Me.ListBox2.Items.AddRange(items)
            End If


    Thanks EM
    Sunday, May 25, 2008 7:47 PM

Answers

  • Well, you could probably get the selected items from the first listbox, and loop through them to add  each item to the second listbox, if not already there.  You would not have to validate the selected collection either, because if nothing were selected then the loop would exit immediately.  Using this method, you would have to re-sort your items, unless you are using auto-sort on the second listbox.

     

     

    Code Snippet

     

    Dim Item As Object

    Dim Items As System.Windows.Forms.ListBox.SelectedObjectCollection

    Items = ListBox1.SelectedItems

        For Each Item In Items

            If ListBox2.Items.IndexOf(Item) = -1 Then

                ListBox2.Items.Add(Item)

            End If

        Next

     

     

    Monday, May 26, 2008 1:13 AM

All replies

  • Well, you could probably get the selected items from the first listbox, and loop through them to add  each item to the second listbox, if not already there.  You would not have to validate the selected collection either, because if nothing were selected then the loop would exit immediately.  Using this method, you would have to re-sort your items, unless you are using auto-sort on the second listbox.

     

     

    Code Snippet

     

    Dim Item As Object

    Dim Items As System.Windows.Forms.ListBox.SelectedObjectCollection

    Items = ListBox1.SelectedItems

        For Each Item In Items

            If ListBox2.Items.IndexOf(Item) = -1 Then

                ListBox2.Items.Add(Item)

            End If

        Next

     

     

    Monday, May 26, 2008 1:13 AM
  • Hi the code below is not doing what I want.  When I add an item form Listbox1 to Listbox2, it deletes the item which is already in Listbox2. 

    Dim Item As Object

    Dim Items As System.Windows.Forms.ListBox.SelectedObjectCollection

    Items = ListBox1.SelectedItems

    For Each Item In Items

    If ListBox2.Items.IndexOf(Item) = -1 Then

    ListBox2.Items.Add(Item)

    End If

    Next

    Saturday, June 7, 2008 5:40 AM
  • ExcelMonkey said:

    Hi the code below is not doing what I want.  When I add an item form Listbox1 to Listbox2, it deletes the item which is already in Listbox2. 


    It is working properly for me, and not deleting anything from Listbox2.  It is not adding duplicate items, only items not already in Listbox2.

    Are you using VB2005 or VB2008, and do you have any other events that fire in Listbox2 that have code that might delete items?

    Saturday, June 7, 2008 10:21 PM