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

Hi,

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.

Thanks,

Victor
vcharlesAsked:
Who is Participating?
 
Fernando SotoConnect With a Mentor RetiredCommented:
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

0
 
Ryan ChongCommented:
try:
Dim items As New List(Of String)
        For Each itm In ListBox1.SelectedItems
            items.Add(itm.ToString())
        Next
        Dim rst As String
        rst = String.Join(",", items.ToArray)
        MessageBox.Show(rst)

Open in new window

0
 
vcharlesAuthor Commented:
Thank You. I will get back to you.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
vcharlesAuthor Commented:
Hi Fernando,

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

Thanks,

Victor
0
 
vcharlesAuthor Commented:
Thank You.
0
 
vcharlesAuthor Commented:
Please disregard question from last post.
0
 
Fernando SotoRetiredCommented:
Not a problem Victor, glad to help.
0
All Courses

From novice to tech pro — start learning today.