?
Solved

ListBox SelectedItems.CopyTo

Posted on 2006-06-09
9
Medium Priority
?
1,908 Views
Last Modified: 2007-12-19
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
Comment
Question by:taxgurl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 16873925
Dim Groups(lstGroupCode.SelectedItems.Count) As Object
lstGroupCode.SelectedItems.CopyTo(Groups, 0)
0
 

Author Comment

by:taxgurl
ID: 16873951
thanks, I tried that as well, but that returns an object of type dataRowView.....  I want an array.
0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 16874035
what is the datasource for your listbox?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:taxgurl
ID: 16874060
it's a dataSet
0
 
LVL 10

Expert Comment

by:vadim63
ID: 16874317
Did you try:

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

Author Comment

by:taxgurl
ID: 16874351
Yep, tried that also - I don't remember the result off hand, but I got an error on that as well.
0
 
LVL 10

Expert Comment

by:vadim63
ID: 16874378
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
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 2000 total points
ID: 16875332
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
 

Author Comment

by:taxgurl
ID: 16885832
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

770 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