[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2016-09-20
7
Medium Priority
?
50 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 54

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 64

Accepted Solution

by:
Fernando Soto earned 2000 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

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 64

Expert Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

873 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