[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 825
  • Last Modified:

Catching Multiple Select Items from a Listbox in VB .Net

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.

 
0
OlympicNigel
Asked:
OlympicNigel
  • 3
  • 2
1 Solution
 
omegaomegaDeveloperCommented:
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.

Cheers,
Randy

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
 
        MsgBox(strCSV)
 
    End Sub
End Class

Open in new window

0
 
OlympicNigelAuthor Commented:
Thanks but this just produces a string with:

"System.Data.DatRowView, System.Data.DatRowView"
0
 
OlympicNigelAuthor Commented:
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.

Nigel
0
 
omegaomegaDeveloperCommented:
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.

Cheers,
Randy

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

Open in new window

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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now