retrieving the items of listbox...

Hi!!!

I need to retrieve the items of a Listbox and assign they to an array. Somebody knows How to do this?

Thanks in advanced

[130 byte] By [JosEloy] at [2007-12-28]
# 1
why dont you load the items first into an array and then bind the listbox to the array?
BlairAllenStark at 2007-9-4 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2

Dim myItems As New ArrayList()

For Each i As String In Me.ListBox1.Items

myItems.Add(i)

Next

Jack2005_MSFT at 2007-9-4 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 3
Jack2005_MSFT wrote:

Dim myItems As New ArrayList()

For Each i As String In Me.ListBox1.Items

myItems.Add(i)

Next

again. . .

load the strings into an arraylist first. . .

then just do

ListBox1.DataSource = MyArrayList

quit moving collections around. . . load them one place and bind your controls to them.

BlairAllenStark at 2007-9-4 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 4

but if you must:

Dim al As New ArrayList(ListBox1.Items)

BlairAllenStark at 2007-9-4 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 5
How do you load the strings into an arraylist? Could you explain that part? I have the same idea as the thread started.. I want to search my listbox items effectively using array..so i want to load all the items into an array..and search using the array..

Since u advised that he/she/we should load the strings in one place and bind the listbox to the array, could you give an example? Thanx..
deathack at 2007-9-4 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 6

Because I can't stand vb and refuse to code in it unless I am getting paid. . . god what a dog. . . this is in C#

List<string> _stringList = new List<string>();

private void Form1_Load(object sender, EventArgs e)

{

_stringList.AddRange(new String[] {"foo","bar"});

listBox1.DataSource = _stringList;

}

BlairAllenStark at 2007-9-4 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 7

Blair it's nice to see you again.

Please DO NOT start the VB stuff. If you don't like VB, I have a really good piece of advice. Stay out of VB fora and you won't be bothered. I won't go in C forum and criticize their language, ok?

And how about not posting C# in a VB Forum?

P.S. Tell BSDetector, I said hello!

ReneeC at 2007-9-4 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 8
Blair Allen Stark wrote:

Because I can't stand vb and refuse to code in it unless I am getting paid. . . god what a dog. . . this is in C#

There is no need for that attitude, C# and VB.NET code gets compiled into same IL assembly. There is no need to express your dislike for VB.

I agree that you should not create new collections but reuse existing collections. I also converted the C# code to VB.

' Create list of strings

Dim _stringList As New List(Of String)(New String() {"foo", "bar"})

' Use list of strings as source for a ListBox control

ListBox1.DataSource = _stringList

AndreasJohansson at 2007-9-4 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...