Solved

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

Posted on 2016-09-20
7
33 Views
Last Modified: 2016-09-21
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
0
Comment
Question by:vcharles
  • 4
  • 2
7 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41807970
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
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41808636
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
 

Author Comment

by:vcharles
ID: 41808659
Thank You. I will get back to you.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:vcharles
ID: 41809551
Hi Fernando,

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

Thanks,

Victor
0
 

Author Closing Comment

by:vcharles
ID: 41809553
Thank You.
0
 

Author Comment

by:vcharles
ID: 41809555
Please disregard question from last post.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 41809581
Not a problem Victor, glad to help.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

13 Experts available now in Live!

Get 1:1 Help Now