Selected Items from one listbox added to a seperate listbox.

Posted on 2005-04-05
Last Modified: 2010-04-23
I am looking for some example code to add the selected items from listbox1 to listbox2.

thank you

Question by:addicktz
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Public Class Form1
        Inherits System.Windows.Forms.Form

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim i As Integer
            For i = 1 To 10
                ListBox1.Items.Add("Item" & i)
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim item As String
            For Each item In ListBox1.SelectedItems
        End Sub

    End Class
    LVL 1

    Author Comment

    is there a way to do it without loops by chance?
    LVL 12

    Accepted Solution

    Ok, im not sure how secure this is... and seemed to work ok when listbox contained strings... im not sure how well it works when adding other objects like more complex classes:

            Dim selItems(ListBox1.SelectedItems.Count - 1) As Object
            ListBox1.SelectedItems.CopyTo(selItems, 0)
    LVL 12

    Expert Comment

    you may want to add tests to make sure items are selected... sorry, forgot to add that

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now