ListBox SelectedItems.CopyTo

I'm trying to use this function to copy all of the selected items in my list box to an array to be used for other purposes.  I get an error "Object cannot be stored in an array of this type" with the following code.  However when I look at the definition of the CopyTo function, it says that it take 2 arguments (System.Array and Index).  

Any ideas on how I can do this?

Dim Groups(lstGroupCode.SelectedItems.Count) As System.Array
lstGroupCode.SelectedItems.CopyTo(Groups, 0)
taxgurlAsked:
Who is Participating?
 
Brian CroweDatabase AdministratorCommented:
the reason you are having problems is that when you set the datasource for a listview to a dataset/datatable/dataview the collection will consist of datarowview objects.  You will just have to work with it

Dim Groups(lstGroupCode.SelectedItems.Count) As datarowview
lstGroupCode.SelectedItems.CopyTo(Groups, 0)

'to access a member of the array

dim value as string
value = groups(index).row("columnname").tostring
0
 
Brian CroweDatabase AdministratorCommented:
Dim Groups(lstGroupCode.SelectedItems.Count) As Object
lstGroupCode.SelectedItems.CopyTo(Groups, 0)
0
 
taxgurlAuthor Commented:
thanks, I tried that as well, but that returns an object of type dataRowView.....  I want an array.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Brian CroweDatabase AdministratorCommented:
what is the datasource for your listbox?
0
 
taxgurlAuthor Commented:
it's a dataSet
0
 
vadim63Commented:
Did you try:

Dim Groups(lstGroupCode.SelectedItems.Count) As String
lstGroupCode.SelectedItems.CopyTo(Groups, 0)
0
 
taxgurlAuthor Commented:
Yep, tried that also - I don't remember the result off hand, but I got an error on that as well.
0
 
vadim63Commented:
It's very strange, b'cause this code works fine:

        Dim arr(ListBox1.SelectedItems.Count) As String
        ListBox1.SelectedItems.CopyTo(arr, 0)
        Dim str As String = ""
        For i As Integer = 0 To UBound(arr) - 1
            str += arr(i) & vbCrLf
        Next
        MsgBox(str)
0
 
taxgurlAuthor Commented:
Thank you very much BriCrowe, this solution works perfect.  And thanks so much for your very quick responses (which is why I valued this question so high).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.