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 want to add text ( string or number) between each listbox

ex:)listbox1 first items is 'A' , listbox2 first items is 'b' , listbox3 first items is 'c'...etc . Now I want listbox10 first item to be "The letters ABC is now done"

The main idea is to combine all listboxes items and adding text between them to create a meaningful sentence

here is my code

Dim controls = New List(Of ListBox)() From _
{ListBox1, ListBox2, ListBox3, ListBox4, ListBox5, ListBox6, ListBox7, ListBox8}
    Dim minCount = Controls.Min(Function(x) x.Items.Count)
    For x = 1 To minCount - 1
        ListBox10.Items.Add(String.Join(" ", controls.Select
        (Function(lb) lb.Items(x).ToString)))
    Next

End Sub

Where i must to add my (string)?

1
You say "between each listbox", but your example "The letters is ABC" just puts text in front of the string. Unclear what you want with that example. If you just want the lead text: ListBox10.Items.Add("The letters is " & String.Join(... - LarsTech
@LarsTech no that was just example sir .. i want to add alot of diffrent text between each listbox in different places - bod
Now you just want text before and after the letters? I'm trying to figure out what's going between the letters... - LarsTech
for example listbox1 first items is 'A' , listbox2 first items is 'b' , listbox3 first items is 'c'...etc . Now I want listbox10 first item to be "The letter A is the letter before B now done C" - bod
If it's going to be different text between the items, then you will have to lose the string.join and loop through your listbox collection. Your for-loop is wrong, btw, it should be 0 to minCount - 1 - LarsTech

1 Answers

2
votes

Since you want to put different text between the listbox items, you would have to drop the String.Join function and loop through the ListBoxes yourself:

Dim sb As New StringBuilder
For i As Integer = 0 To controls.Count - 1
  For j As Integer = 1 To minCount - 1
    sb.Append(controls(i).Items(j).ToString)
  Next
  If i < controls.Count - 1 Then
    sb.Append(" " & i.ToString & " ")
  End If
Next
ListBox10.Items.Add(sb.ToString)