• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2008
  • Last Modified:

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)
0
taxgurl
Asked:
taxgurl
  • 4
  • 3
  • 2
1 Solution
 
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
 
Brian CroweDatabase AdministratorCommented:
what is the datasource for your listbox?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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
 
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.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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