在列表框之间共享项目 [英] Share items between listbox

查看:105
本文介绍了在列表框之间共享项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

如何在列表框之间共享项目?

如果我有4个列表框,其中一个包含20个项目,之后我将其分为4个,所以每个每个包含5个项目。请问,我该怎么办呢?



我的尝试:



hello,
How do i share item between listbox?
If i have 4 listbox and one of them contains 20 items and i later divide it between 4 so each contain 5 item each. Please, how do i go about it?

What I have tried:

dim j as integer=0
For i as integer=0 to list1.items.count - 1
If j=5 then
Exit sub
End if
List2.items.add(list1.items(i).tostring())
List1.items.removeat(i)
J+=1
Next

推荐答案

如果你要删除项目,你需要在列表框的另一端开始。



如果你想一个清单共有3件商品
You need to start "at the other end" of the listbox if you are going to remove items.

If you think about a list with 3 items
Item 1 = "A"
Item 2 = "B"
Item 3 = "C"

如果你然后删除第2项以下的所有内容,那么

If you then delete Item 2 everything below that shuffles up so

Item 1 = "A"
Item 2 = "C"

第3项不再存在。

而不是 j 作为一个单独的计数器 - 有一个子程序,它将项目n添加到列表框并从原始文件中删除 - 然后在循环中调用

Item 3 no longer exists.
Instead of having j as a separate counter - have a subroutine that adds items n to m to a listbox and removes from the original - then call that within a loop


看看下面的代码:



Take a look at below code:

'variables
Dim i As Integer, j As Integer, k As Integer
'destination listbox: ListBox2 to 4
Dim dstBox As ListBox = Nothing
'source listbox
Dim srcBox As ListBox = Me.ListBox1

'loop through listboxes
For i = 2 To 4 
	'set destination listbox
	dstBox = DirectCast(Me.Controls(String.Concat("ListBox", i)) ,ListBox)
	'move items
	For j = 1 To 5 
	 	'copy last item from ListBox1 to ListBoxN
		dstBox.Items.Add(srcBox.Items(srcBox.Items.Count()-1))
		'remove last item from ListBox1
		srcBox.Items.RemoveAt(srcBox.Items.Count()-1)
	Next 'j
Next 'i


这篇关于在列表框之间共享项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆