Results 1 to 7 of 7

Thread: [RESOLVED] [2005] Moving items from ListBox1 to ListBox2

  1. #1

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Resolved [RESOLVED] [2005] Moving items from ListBox1 to ListBox2

    Hi there,

    I have two listboxes, we'll call them ListBox1 and ListBox2. I have a button on my Form, that calls the following bit of code when clicked:

    Code:
    For Each currentItem in ListBox1.SelectedItems
            ListBox2.Items.Add(currentItem)
            ListBox1.Items.Remove(currentItem)
    Next

    This code doesn't work as it starts to complain about the selection having changed. What would be the preferred method to accomplish something like this? I'd be happy to get the whole selection from ListBox1 and bulk add/erase it. Does the ListBox object provide this kind of functionality?

    Another Q, what is the difference between SelectionMode 'MultiSimple' and 'MultiExtended'. I have currently set it to MultiSimple on both ListBoxes.

    Thanks,
    Mightor

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Moving items from ListBox1 to ListBox2

    Create an array containing all the selected items from ListBox1, then remove those items from ListBox1, then add them to ListBox2.

    As for the ListBox.SelectionMode property, if you go to the MSDN library for that property it will tell you that it is type SelectionMode. If you click on the name it will take you to the help topic for that type, which describes each of the possible values.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Re: [2005] Moving items from ListBox1 to ListBox2

    Have you tried it in an If... Then statement?

    Code:
    dim x as String
    
    x = ListBox1.SelectedItem
    
    If x = CStr("") Then
    Messagebox.Show("Pick something")
    Else
    Listbox2.Items.Add(x)
    ListBox1.Items.Remove(x)
    End If
    Just a suggestion
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

  4. #4

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Re: [2005] Moving items from ListBox1 to ListBox2

    I've figured out what the problem is. As soon as I remove the item from the ListBox1, the total selection of items changes because I've just taken one from it. I need to build an index of the items that need to be moved and remove them in reverse order, ie starting with the highest down to the lowest. or I'll end up changing the index numbers of the the rest of the items that need to be removed.
    I've found info on this on http://www.dotnet247.com/247referenc.../11/55302.aspx. It boils down to being able to add items in bulk, just not being able to remove them in the same fashion. It just means the code for this is going to be a little convoluted.

    Edit: the code for removing ends up looking something like this (not tested):
    Code:
    Dim nIndex as Integer
    For nIndex = ListBox1.SelectedItems.Count - 1 to 0 Step -1
            ListBox1.Items.Remove(ListBox1.SelectedItems(nIndex))
    Next nIndex
    Last edited by mightor; Apr 24th, 2007 at 01:14 AM.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Moving items from ListBox1 to ListBox2

    Your original problem stems from the fact that you cannot enumerate a collection and change it. A For Each loop creates an enumerator so you cannot change the collection within the loop. That doesn't mean that you can't use a loop though:
    vb Code:
    1. Dim item As Object
    2.  
    3. While Me.ListBox1.SelectedItems.Count > 0
    4.     item = Me.ListBox1.SelectedItems(0)
    5.     Me.ListBox1.Items.Remove(item)
    6.     Me.ListBox2.Items.Add(item)
    7. End While
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Moving items from ListBox1 to ListBox2

    Quote Originally Posted by jmcilhinney
    Your original problem stems from the fact that you cannot enumerate a collection and change it. A For Each loop creates an enumerator so you cannot change the collection within the loop. That doesn't mean that you can't use a loop though:
    vb Code:
    1. Dim item As Object
    2.  
    3. While Me.ListBox1.SelectedItems.Count > 0
    4.     item = Me.ListBox1.SelectedItems(0)
    5.     Me.ListBox1.Items.Remove(item)
    6.     Me.ListBox2.Items.Add(item)
    7. End While
    Very Well done!

  7. #7

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Re: [2005] Moving items from ListBox1 to ListBox2

    Quote Originally Posted by jmcilhinney
    Your original problem stems from the fact that you cannot enumerate a collection and change it. A For Each loop creates an enumerator so you cannot change the collection within the loop. That doesn't mean that you can't use a loop though:
    vb Code:
    1. Dim item As Object
    2.  
    3. While Me.ListBox1.SelectedItems.Count > 0
    4.     item = Me.ListBox1.SelectedItems(0)
    5.     Me.ListBox1.Items.Remove(item)
    6.     Me.ListBox2.Items.Add(item)
    7. End While
    Ah that works perfectly! Thanks. It's beautifully simple and I can't believe I didn't think to deal with it this way.

    Thanks a lot!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width