Moving items from one list box to another

MasterChief777
01-01-2004, 12:13 PM
Hi,

In my form I have two listboxes and two buttons. One of the buttons is to move the selected items from listbox1 to listbox2 and the other is to move the selected items from listbox2 to listbox1.

Here's the code I tried to get it working:


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox2.Items.Add(ListBox1.SelectedItems)
ListBox1.Items.Remove(ListBox1.SelectedItems)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ListBox1.Items.Add(ListBox2.SelectedItems)
ListBox2.Items.Remove(ListBox2.SelectedItems)
End Sub


If someone could point out what I'm doing wrong and what I should do instead, I'd really appreciate it.

Thanks.

AFterlife
01-01-2004, 12:32 PM
This code will add one at a time


Dim n As Integer
For n = 0 To ListBox1.Items.Count - 1
ListBox2.Items.Add(ListBox1.Items.Item(n))
Next

This code will add them all

Dim n As Integer
For n = 0 To ListBox1.Items.Count - 1
ListBox2.Items.Add(ListBox1.Items.Item(n))
Next

MasterChief777
01-01-2004, 12:41 PM
This code will add one at a time


Dim n As Integer
For n = 0 To ListBox1.Items.Count - 1
ListBox2.Items.Add(ListBox1.Items.Item(n))
Next

This code will add them all

Dim n As Integer
For n = 0 To ListBox1.Items.Count - 1
ListBox2.Items.Add(ListBox1.Items.Item(n))
Next

Thanks for the reply. What I'm trying to do though is to move all selected items between listboxes, where it's possible to select several non-adjacent items.

AFterlife
01-01-2004, 12:48 PM
The first code will do that. It adds the item you select. doesnt matter what order it is in. Or you can do it this way.Or are you using multiple select?

That will also work. There are many many ways.
Dim n As Integer
n = ListBox1.SelectedIndex
ListBox2.Items.Add(ListBox1.SelectedItems(0))

AFterlife
01-01-2004, 12:56 PM
If thats the case then this is the code you are looking for.
Works for multiselecteditems in any order.I think this is what you need.
dim i as integer
For i = 0 To ListBox1.SelectedItems.Count - 1
ListBox2.Items.Add(ListBox1.SelectedItems.Item(i))
Next i

MasterChief777
01-01-2004, 01:38 PM
That helped. Thanks a lot.

AFterlife
01-01-2004, 01:43 PM
Np.

MasterChief777
01-01-2004, 01:56 PM
I have another question now--I've added a textbox to my form. What I want this to do is to display the last selected item. So my listbox has item1, item2,....,item10. What I want it to do is to display the last item I select in the text box, and once again there can be multiple selections.

I tried to update the textbox in the listboxe's click sub:


Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
TextBox1.Text = (ListBox1.Items.Item(ListBox1.SelectedIndex))
End Sub


This works well when I only select one item at a time but when I select more than one, it only shows the first item i selected during the multiple select.

MasterChief777
01-01-2004, 01:59 PM
Ok, nevermind that. I just solved it like this:


Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
TextBox1.Text = ListBox1.SelectedItems.Item(ListBox1.SelectedItems.Count - 1)
End Sub

reboot
01-01-2004, 02:07 PM
By the wayDim li As String()
ReDim li(ListBox1.SelectedItems.Count - 1)

ListBox1.SelectedItems.CopyTo(li, 0)

ListBox2.Items.AddRange(li)

AFterlife
01-01-2004, 02:40 PM
Neat, i like that one.

Csharp
01-02-2004, 09:47 AM
redim is a bit of a left over vb6 function. ( after a long while using .net , you tend to avoid using pre - .net functions as .net has some really great in-built stuff ) , here's a quick example of copying selected items from one listbox to another...

'/// here we'll enumerate all selected items in ListBox1 ( if any are selected that is ).
Dim lbCol As New ListBox.SelectedObjectCollection(ListBox1)
'/// then we'll add all the selected items in one go to ListBox2.
ListBox2.Items.AddRange(ArrayList.Adapter(lbCol).ToArray)
'/// please note , if you don't want duplicates ( when selecting the same items a second time )
'/// you will still have to loop through the listbox to remove the items, before re-adding them.

if you want to make sure you dont have duplicates ....

Dim lbCol As New ListBox.SelectedObjectCollection(ListBox1)
Dim strItem As String
For Each strItem In ArrayList.Adapter(lbCol).ToArray
If ListBox2.Items.IndexOf(strItem) > -1 Then '/// if it's also in listbox2.
MessageBox.Show(ListBox2.Items(ListBox2.Items.IndexOf(strItem)))
ListBox2.Items.RemoveAt(ListBox2.Items.IndexOf(strItem))
'/// you can remove the selected items from listbox2 using the indexof ListBox2.SelectedItems.IndexOf(strItem)
End If
Next
'/// now we can add the selected items.
ListBox2.Items.AddRange(ArrayList.Adapter(lbCol).ToArray)

reboot
01-02-2004, 10:11 AM
While I agree your solution is better (some of us are still in learning mode), Redim is part of the Visual Basic.Net language. It doesn't require either Microsoft.VisualBasic or Microsoft.VisualBasic.Compatibility. As opposed to something like IsNumeric, which is indeed 'leftover' from VB6 and requires the compatibility layer. Just wanted to make that point clear.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum