Catching Multiple Select Items from a Listbox in VB .Net

Posted on 2009-04-24
Last Modified: 2013-12-26
How do you read the values of multiple selected objects in a Listbox whose values are generated from a dbase/dataset?

I would like to read the values of each selected item into a single SCV file with each value seperated with a comma.

Question by:OlympicNigel
    LVL 12

    Expert Comment

    Hello, OlympicNigel,

    I'm not sure what you mean by "values", but perhaps you are just looking for the "SelectedItems" property.  If the values were those generated by ToString, you cold do something like the example in the snippet to generate a csv string.  Then just write that to whatever file you like.


    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim strCSV As String = ""
            For Each objSelected As Object In Me.ListBox1.SelectedItems
                strCSV = strCSV & objSelected.ToString & ","
            Next objSelected
            If (strCSV <> "") Then
                strCSV = strCSV.Substring(0, strCSV.Length - 1)
            End If
        End Sub
    End Class

    Open in new window


    Author Comment

    Thanks but this just produces a string with:

    "System.Data.DatRowView, System.Data.DatRowView"

    Author Comment

    Maybe I should explain more. The Listbox has 5 items: Cat1, Cat2, Cat3, Cat4 and Cat5.

    I would like to be able to read the selected items into a CSV string, so for example if 1, 2 and 5 where selected, the string would look like this:

    "Cat1, Cat2 Cat5"

    The items within the listbox are generated by a dataset. If you do this with a hard typed list it works, but when you use the dataset to populate the listbox options then the code produces the snippet in the above post.

    I can not seem to be able to get the Cat1, Cat2 etc only the object titles...hope that makes more sense.

    LVL 12

    Accepted Solution

    Hello, OlympicNigel,

    OK, I guess that you mean that Cat1, Cat2, Cat3, Cat4 and Cat5 are displaying in the ListBox.  (The ListBox actually has (contains) the records from which these values are taken.)

    You just need to modify the loop slightly to get the results that you want.  I don't know the details of the database records that you have bound to the list, but (for example) if the DisplayMember property in your list were "Field3", then you would modify your loop to match that in the attached snippet.

    By the way, you could use the name of any field from the bound record set to get a csv string of those values for the selected records.

    Hope that helps.


            For Each drvSelected As DataRowView In Me.ListBox1.SelectedItems
                strCSV = strCSV & drvSelected.Row.Item("Field3").ToString & ","
            Next drvSelected

    Open in new window


    Author Closing Comment

    Thank you so much. This had me going round in circles and is just what I needed.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    734 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

    23 Experts available now in Live!

    Get 1:1 Help Now