How to convert the items in a listbox to an arraylist?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I want to make an arraylist of all the items in a listbox.

Can I do it like this:

CType(lstSelectedList.DataSource, ArrayList)

Or is there a better way in VB.NET (Framework 1.1)

thanks

Philip
 
Hi,

I want to make an arraylist of all the items in a listbox.

Can I do it like this:

CType(lstSelectedList.DataSource, ArrayList)

Or is there a better way in VB.NET (Framework 1.1)

Maybe ListBox.Items.CopyTo would work for what you need? Do you know
what each item in the ListBox actually is? Strings? Ints? DataRows?
 
Hi,

I tried to do it using 'CopyTo' but I got this compile error:

'System.Collections.ArrayList' cannot be converted to '1-dimensional array
of System.Object'.

Here is the code I tried:
lstSelectedList.Items.CopyTo(m_arrSelList, 0)

thanks for any help

Philip
 
Hi, thanks,

all the items in the listbox are string values.

Then you should be able to create a string array equal to the number of
items in your listbox and use the CopyTo method to copy the items into
the string array.
 
Then you should be able to create a string array equal to the number of
items in your listbox and use the CopyTo method to copy the items into
the string array.

Sorry -- clarification:

You can copy the items into a string array and then create an arraylist
from the string array. Something like this:

Dim s(ListBox1.Items.Count) As String
Dim al As New ArrayList

ListBox1.Items.CopyTo(s, 0)
al = New ArrayList(s)
 
Back
Top