Solved

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

Posted on 2016-09-20
7
31 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank You. I will get back to you.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:vcharles
Comment Utility
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
Comment Utility
Thank You.
0
 

Author Comment

by:vcharles
Comment Utility
Please disregard question from last post.
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Not a problem Victor, glad to help.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

12 Experts available now in Live!

Get 1:1 Help Now