Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello
I have 3 listboxes each one contains many items (listbox1 , listbox2,listbox3)
and another empty listbox (listbox4)
im trying to add first three listboxes items into the 4th listbox

ex: ) listbox1 first items is 'A' , listbox2 first items is 'b' , listbox3 first items is 'c'
now listbox4 first item must be "Abc"

The problem is Its work but keep loop the items.

here is my code :

VB
For i = 1 To ListBox1.Items.Count - 1

           For j = i To ListBox2.Items.Count - 1

               For k = j To ListBox3.Items.Count - 1

                   ListBox4.Items.Add(ListBox1.Items(j).ToString + ListBox2.Items(j).ToString + ListBox3.Items(k).ToString)

                   Exit For
               Next
           Next
       Next
Posted
Updated 15-Dec-15 2:37am
v3
Comments
Rajdeep Debnath 15-Dec-15 8:21am    
listbox1, listbox2, listbox3 have same no of items?
Leo Chapiro 15-Dec-15 8:28am    
And what is your problem, have you got an error? Further if you want to add only the FIRST ITEMS so you don't need any loop.
Member 12208071 15-Dec-15 8:35am    
I need all the items not just the first , the problem is items keep looping inside the listbox4
Leo Chapiro 15-Dec-15 9:05am    
What does it mean: items keep looping inside the listbox4? What is the result of adding the items?

An alternative using Linq

C#
Dim l1 As List(Of String) = ListBox1.Items.Cast(Of String)().ToList()
Dim l2 As List(Of String) = ListBox2.Items.Cast(Of String)().ToList()
Dim l3 As List(Of String) = ListBox3.Items.Cast(Of String)().ToList()

Dim l4 As List(Of String) = New List(Of String)(l1)
l4.AddRange(l2)
l4.AddRange(l3)

ListBox4.DataSource = l4
 
Share this answer
 
Your code might work if all the list boxes had equal number of items. Consider this approach:

VB.NET
Dim list As New List(Of ListBox)
list.AddRange({ListBox1, ListBox2, ListBox3})
For Each l As ListBox In list
    For Each s As String In l.Items
        ListBox4.Items.Add(s)
    Next
Next


Hope this helps.

regs
 
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