Help with selecting multiple rows from a listbox and assigning values to a variable using VB.NET


How do you select multiple rows from a listbox and assigning values to a variable using VB.NET?  For example if I select
House, Garage, Car from the listbox, would like to select a string variable s = House,Garage,Car.


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
Dim items As New List(Of String)
        For Each itm In ListBox1.SelectedItems
        Dim rst As String
        rst = String.Join(",", items.ToArray)

Open in new window

Fernando SotoRetiredCommented:
Hi Victor;

The following code snippet should do what you need.
'' This linq query will iterate through the selected items in the 
'' ListBox control and build a comma seperated string of the selected values
'' The variable acc is the string that is being built and item is the next item in the ListBox 
Dim s As String = (From r In ListBox1.SelectedItems
                   Select r).Aggregate(Function(acc As String, item As String) acc & ", " & item)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
Thank You. I will get back to you.
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

vcharlesAuthor Commented:
Hi Fernando,

Your solution works, is there a way to select multiple rows from the listbox without pressing the ctrl key?


vcharlesAuthor Commented:
Thank You.
vcharlesAuthor Commented:
Please disregard question from last post.
Fernando SotoRetiredCommented:
Not a problem Victor, glad to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.