Selected Items from one listbox added to a seperate listbox.

Posted on 2005-04-05
Medium Priority
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
  • 2
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13712896
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

Author Comment

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

Accepted Solution

S-Twilley earned 2000 total points
ID: 13713002
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

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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month14 days, 8 hours left to enroll

840 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