Solved

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

Posted on 2016-09-20
7
40 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 52

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 63

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 63

Expert Comment

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

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

728 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