1

I am able to only move single items from one listbox to another with this code. I tried with both MultiSimple & MultiExtended SelectionMode.

How do I select multiple items and then move them?

Private Sub cmdAdd_Click(ByVal sender As System.Object, 
                         ByVal e As System.EventArgs
                        ) Handles cmdAdd.Click

        Dim i As Integer = Listbox1.SelectedIndex

        If i = -1 Then
            Exit Sub 'skip if no item is selected
        End If

        Listbox2.Items.Add(Listbox1.Items(i)) 
        Listbox1.Items.RemoveAt(i)

    End Sub

3 Answers 3

2

You need to use SelectedIndices or SelectedItems.

Private Sub cmdAdd_Click(ByVal sender As System.Object, 
                         ByVal e As System.EventArgs
                        ) Handles cmdAdd.Click

    Dim selectedItems = (From i In ListBox1.SelectedItems).ToArray()

    For Each selectedItem In selectedItems

        Listbox2.Items.Add(selectedItem)
        Listbox1.Items.Remove(selectedItem)

    Next

End Sub

Note the use of a Linq query to get list of selected items as an array. Using an array is required to prevent "Collection changed" exceptions. You may need to add a reference to System.Linq.

Sign up to request clarification or add additional context in comments.

7 Comments

@ Tim Murphy - Thanks for the reply. I get this error when I use the above code - 'ToArray' is not a member of 'System.Windows.Forms.ListBox.SelectedObjectCollection'. I also added System.Linq as reference
Are you importing System.Linq? You can do it via the project or at the top of the code file: Imports System.Linq.
@ Tim Murphy - yes, at the top of the code line I have mentioned Imports System.Linq
I've fixed the code. Sorry for the stuff around but I was answering last night on a PC without VS.
@ Tim Murphy - Thanks Tim. Works beautifully!
|
0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ComboBox1.Items.Add("SanDiego")
    ComboBox1.Items.Add("BeverlyHills")
    ComboBox1.Items.Add("Florida")
    ComboBox1.Items.Add("NewYork")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim s As String
    s = ComboBox1.SelectedItem

    ListBox1.Items.Add(s)
    ComboBox1.Items.Remove(s)
End Sub

1 Comment

Adding an explanation to your code will help the OP understand what you did and why.
0
Private Sub cmdAdd_Click(ByVal sender As System.Object, 
                     ByVal e As System.EventArgs
                    ) Handles cmdAdd.Click


For Each selectedItem In (From i In ListBox1.SelectedItems).Tolist()


    Listbox2.Items.Add(selectedItem)
    Listbox1.Items.Remove(selectedItem)

Next

End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.