简体   繁体   English

如何将3个列表框项目添加到一个列表框中

[英]how to Add 3 listboxes items into one listbox

I'm using vb.net form. 我正在使用vb.net表单。

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 我有3个列表框,每个包含许多项(listbox1,listbox2,listbox3)和另一个空的列表框(listbox4),我试图将前三个列表框项添加到第四个列表框

ex: ) listbox1 first items is 'A' , listbox2 first items is 'b' , listbox3 first items is 'c' now listbox4 first item must be "Abc" 例如:)listbox1的第一项是'A',listbox2的第一项是'b',listbox3的第一项是'c'现在listbox4的第一项必须是“ Abc”

The problem is I't's work but keep loop the items. 问题是我不上班,但要保持循环。 here is my code : 这是我的代码:

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

You have nested the loops. 您已经嵌套了循环。 This means that the inner loops are repeated at each loop of the outer loops. 这意味着内循环在外循环的每个循环处重复。 Eg If the three list boxes have 3, 4, and 6 elements the most inner loop will loop 3 x 4 x 6 = 72 times! 例如,如果三个列表框具有3、4和6个元素,则最内部的循环将循环3 x 4 x 6 = 72次!

Instead make only one loop: 而是只做一个循环:

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
    ListBox4.Items.Add( _
        ListBox1.Items(i).ToString + _
        ListBox2.Items(i).ToString + _
        ListBox3.Items(i).ToString)
Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM