0

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
2
  • Looking at your Math.Min expression I presume that your listboxes contain a different quantity of items. Right? Commented Dec 16, 2015 at 13:11
  • @Steve no all of them with same quantity items Commented Dec 16, 2015 at 13:17

1 Answer 1

4

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)

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks for the kind support, but alot of errors occur when trying above code
Sorry, this is C#, I will fix the answer for Vb.NET
Don't worry at all :)
All right, that should be working as well for VB.NET (Do not forget to Imports System.Linq )
So much appreciate your help .. but im trying to add the refrence linq .. but cant find it at all .. ill keep trying .. many thanks
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.