Solved

ListBox SelectedItems.CopyTo

Posted on 2006-06-09
9
1,828 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
 

Author Comment

by:taxgurl
ID: 16874060
it's a dataSet
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now