Solved

ListBox SelectedItems.CopyTo

Posted on 2006-06-09
9
1,863 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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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 …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

860 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