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

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
 
P

Patrick Steele

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?
 
G

Guest

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
 
P

Patrick Steele

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

Patrick Steele

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top