Link to home
Start Free TrialLog in
Avatar of taxgurl
taxgurl

asked on

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)
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

Dim Groups(lstGroupCode.SelectedItems.Count) As Object
lstGroupCode.SelectedItems.CopyTo(Groups, 0)
Avatar of taxgurl
taxgurl

ASKER

thanks, I tried that as well, but that returns an object of type dataRowView.....  I want an array.
what is the datasource for your listbox?
Avatar of taxgurl

ASKER

it's a dataSet
Did you try:

Dim Groups(lstGroupCode.SelectedItems.Count) As String
lstGroupCode.SelectedItems.CopyTo(Groups, 0)
Avatar of taxgurl

ASKER

Yep, tried that also - I don't remember the result off hand, but I got an error on that as well.
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)
ASKER CERTIFIED SOLUTION
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of taxgurl

ASKER

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