|
-
Apr 24th, 2007, 12:53 AM
#1
Thread Starter
Lively Member
[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
-
Apr 24th, 2007, 01:01 AM
#2
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.
-
Apr 24th, 2007, 01:02 AM
#3
Addicted Member
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.
-
Apr 24th, 2007, 01:10 AM
#4
Thread Starter
Lively Member
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.
-
Apr 24th, 2007, 01:11 AM
#5
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:
Dim item As Object
While Me.ListBox1.SelectedItems.Count > 0
item = Me.ListBox1.SelectedItems(0)
Me.ListBox1.Items.Remove(item)
Me.ListBox2.Items.Add(item)
End While
-
Apr 24th, 2007, 01:17 AM
#6
Re: [2005] Moving items from ListBox1 to ListBox2
 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:
Dim item As Object
While Me.ListBox1.SelectedItems.Count > 0
item = Me.ListBox1.SelectedItems(0)
Me.ListBox1.Items.Remove(item)
Me.ListBox2.Items.Add(item)
End While
Very Well done!
-
Apr 24th, 2007, 01:20 AM
#7
Thread Starter
Lively Member
Re: [2005] Moving items from ListBox1 to ListBox2
 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:
Dim item As Object
While Me.ListBox1.SelectedItems.Count > 0
item = Me.ListBox1.SelectedItems(0)
Me.ListBox1.Items.Remove(item)
Me.ListBox2.Items.Add(item)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|