本文介绍了如何将3个列表框项添加到一个列表框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好 我有3个列表框,每个列表框包含很多项目(listbox1,listbox2,listbox3) 和另一个空列表框(listbox4) 我试图将前三个列表框项目添加到第4个列表框中 ex:)listbox1第一项是'A',listbox2第一项是'b',listbox3第一项目是'c' 现在listbox4第一项必须是Abc 问题是它的工作但保持循环项目。 br /> 这是我的代码:
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 :
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推荐答案
使用Linq的替代方案 An alternative using Linq 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
如果所有列表框都有相同数量的项目,您的代码可能会起作用。考虑这种方法: Your code might work if all the list boxes had equal number of items. Consider this approach: 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
希望这会有所帮助。 regs
Hope this helps. regs
更多推荐
如何将3个列表框项添加到一个列表框中
发布评论