Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i am new in programming and i wanna ask you why the code below it just return me one value from entire Listbox can someone help me to get all the values from Listbox.Thnx

Public ps As String

For Each item In lsFoodandBaverages.Items
ps = item.ToString

Next
Posted

Let me guess - it's the last item that's returned?

This is because you aren't doing anything with ps... therefore your code goes through the loop until you reach the last item, which is then stored in ps... Which I assume you use outside the loop.

If you want to do something with each item, you need to put the code inside the loop.
 
Share this answer
 
I have 2 Forms and i wanna populate a listbox2 in Form2 with the item from listbox1 in Form1 and thats why i wanted to use global variable but i think i need to use arrays here
 
Share this answer
 
I solved it :)

array = New String(lsFoodandBaverages.Items.Count - 1) {}
For i As Integer = 0 To lsFoodandBaverages.Items.Count - 1
Dim s As Object = lsFoodandBaverages.Items(i)
array(i) = s.ToString()



and i called this in Listbox2 in Form2

Me.ListBox1.Items.AddRange(array)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900