VB .Net VS 2005 List Box Multiple Select

Posted on 2009-04-24
Last Modified: 2013-12-26
Hi, I am trying to do what should be a simple thing with a list box with mutliple selected values and can not seem to get anywhere near what I want as the result.

Basically I have a mutli select list box with 10 categories which are "cat 1" through to "cat 10".

What I would like to do is check which items in the list box are selected and then have them added to a csv string. So, the output for selected items 1, 3 and 5 would be "cat1, Cat3, Cat5".

I have played with this for hours now and just do not seem to ba able to make progress.

Have checked and tried many of the posting already listed but none seem to work.
Question by:OlympicNigel
    LVL 39

    Accepted Solution

    What about the following (not tested)

    Dim sCSV As String = ""
    For Each cat As Object In listBox1.SelectedItems
        sCSV &= cat.ToString()
        sCSV &= ","
    sCSV = sCSV.Trim(New Char() {","C})

    Open in new window

    LVL 39

    Expert Comment

    Tested, seems to work for me... :). Let me know if you have trouble implementing it for yourself.

    Author Comment

    Abel, Thats great.

    The next question is how does it work with a databound set of items?

    All I get back is:

    "Systen.Data.DataRowView, Systen.Data.DataRowView"
    LVL 39

    Expert Comment

    > The next question is how does it work with a databound set of items?

    please, stick to one question at the time. See here: if you need some assistance.

    Unless it is related and you can't get the solution to work without it. But then I don't understand your question, I'm afraid. Where are you on? Can you show the code you have got so far? Is this ASP.NET or VB.NET forms?


    Author Comment


    Apologies for the wrong use of the site. I am new and had not made myself fully aware of the rules etc.

    Apologies and thank you for pointing out my error.

    LVL 39

    Expert Comment

    No worries. Sometimes it is not as clear cut, but we are trying to keep it simple, as experts, so that we know that when we start answering to a question, that we also know it ends when the question is resolved. But sometimes (or just as often), the asker needs just a bit more guidance to get it working, or forgot to mention some details about his situation.

    Tx for the points, and glad that this bit is sorted ;-). On to the next!

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    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…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    732 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

    17 Experts available now in Live!

    Get 1:1 Help Now