0
votes

I'm using vb.net form.

I have 8 listboxes (listbox1 , listbox2,listbox3..) each one contains many items and another empty listbox (listbox10). I'm trying to add first listboxes items into the listbox10

ex: ) listbox1 first items is 'A' , listbox2 first items is 'b' , listbox3 first items is 'c'...etc . Now listbox10 first item must be "Abc..."

The problem is i'm only able to add 3 listboxes into the listbox10 But i want to all 8 listboxes to include in the loop.

here is my code

Dim n As Integer = _
Math.Min(ListBox1.Items.Count, Math.Min(ListBox2.Items.Count,    ListBox3.Items.Count))

For i As Integer = 0 To n - 1
ListBox10.Items.Add( _
    ListBox1.Items(i).ToString + _
    ListBox2.Items(i).ToString + _
    ListBox3.Items(i).ToString)
Next
1
Looking at your Math.Min expression I presume that your listboxes contain a different quantity of items. Right?Steve
@Steve no all of them with same quantity itemsbod

1 Answers

4
votes

Create a List<Listbox> of all the listboxes that you want to work on. Then, using Linq, is pretty easy to reach your objective

Dim controls = new List(Of Listbox)() From _
      { listbox1, listbox2, listbox3, ....etc.... }
Dim minCount = controls.Min(Function(x) x.Items.Count)
for x = 0 to minCount-1
    listbox10.Items.Add(string.Join(" ", controls.Select(Function(lb) lb.Items(x).ToString)))
Next

Of course, if all the Listbox have the same amount of items then the calculation to find the listbox with the lower amount of items is useless, you could replace it using the Items.Count of any Listbox (However, in defensive programming, I would leave it)