多选列表框
我的表单上有一个列表框,它可以很好地满足我想要做的事情。
我想要编辑表单上的项目,这意味着填充列表框,然后选择相关项目。
我的列表框包含项目尺寸列表,我想选择属于正在编辑的项目的尺寸。
请有人给我一些指点。
我尝试了 me.lstItemSizes.SetSelected(i,true)
但这仅适用于单个项目。
任何帮助将不胜感激。
我的代码:
Private Sub SelectItemSizes(ByVal itemID As Integer)
Dim itemSizes As IList(Of ItemSize) = _sizeLogic.GetItemSizes(itemID)
Me.lstItemSizes.SelectionMode = SelectionMode.MultiExtended
If (itemSizes.Count > 0) Then
For i As Integer = 0 To Me.lstItemSizes.Items.Count - 1
For x As Integer = 0 To itemSizes.Count - 1
If (CType(Me.lstItemSizes.Items(i), PosSize).SizeID = itemSizes(x).SizeID) Then
Me.lstItemSizes.SetSelected(i, True)
Else
Me.lstItemSizes.SetSelected(i, False)
End If
Next
Next
End If
End Sub
I have a list box on a form and it works fine for what I want to do.
I am wanting to edit items on the form, this means populating the listbox and then selecting the relevant items.
My listbox contains a list of item sizes, i want to select the sizes which belong to the item being edited.
PLease can someone give me some pointers.
I tried me.lstItemSizes.SetSelected(i,true)
but this only works for a single item.
Any help wil be much appreciated.
My Code:
Private Sub SelectItemSizes(ByVal itemID As Integer)
Dim itemSizes As IList(Of ItemSize) = _sizeLogic.GetItemSizes(itemID)
Me.lstItemSizes.SelectionMode = SelectionMode.MultiExtended
If (itemSizes.Count > 0) Then
For i As Integer = 0 To Me.lstItemSizes.Items.Count - 1
For x As Integer = 0 To itemSizes.Count - 1
If (CType(Me.lstItemSizes.Items(i), PosSize).SizeID = itemSizes(x).SizeID) Then
Me.lstItemSizes.SetSelected(i, True)
Else
Me.lstItemSizes.SetSelected(i, False)
End If
Next
Next
End If
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否将选择模式设置为多重?
您需要指定它才能允许多项选择。
然后你可以这样做:
这是一个屏幕截图:
在列表框中设置属性后,然后调用 setselected基于您想要选择的值。
在这里您可以添加 20 个数字,并且只选择偶数。
第三次编辑
看看你循环的方式,假设我创建一个整数列表,我的 vb.net 生锈了,我主要用 C# 开发。但假设您这样做了:
您的列表中只有三个项目,因此首先根据列表中的项目循环,然后在列表框中的项目中,反之亦然。看一下:
在代码中,我的 l 是一个仅包含 3 个项目的列表:2、6 和 20。
我将这些项目添加到 l 这只是一个列表对象。
所以现在我必须循环使用这 3 个数字并与我的列表框进行比较。与在列表框上循环然后考虑列表对象相反。
请注意,在我的 for 循环中,一旦找到列表中的项目,我就不再需要循环,因此我
退出
。这确保了我不会逾期所需的循环量。找到该项目后,退出并返回到列表对象计数。运行我的代码后,结果是
Did you set the selectionmode to multi?
You need to specify that in order to allow multiple selections.
Then you can do:
Here is a screen shot:
After you set the property on the listbox then call setselected based on the values you want selected.
Here you can add 20 numbers and only select the even.
3rd edit
Look at the way you are looping, lets assume I create a list of integers, my vb.net is rusty I mainly develop in C#. But assume you did this:
You only have three items in your list, so first loop based on the items on your list, then within the items in your listbox, you have it vice versa. Look at this:
In the code my l is a list of just 3 items: 2, 6, and 20.
I add these items to l which is just a list object.
So now I have to loop using these 3 numbers and compare with my listbox. You have it the opposite you are looping on your listbox and then taking into account the list object.
Notice in my for loop that once the item in my list is found I no longer need to loop so I
exit for
. This ensures I dont overdue the amount of looping required. Once the item is found get out and go back to the count of your list object count.After running my code here is the result
您必须更改
ListBox.SelectionMode
属性以启用多重选择。可能的值由
SelectionModeenum
,如下:
因此,您只需将以下行添加到已有的代码中:
或者,您可以在设计时设置
SelectionMode
属性,而不是使用代码来设置。You have to change the
ListBox.SelectionMode
property in order to enable multiple-selection.The possible values are given by the
SelectionMode
enum, as follows:So, you simply need to add the following line to the code you already have:
Alternatively, you can set the
SelectionMode
property at design time, instead of doing it with code.根据 MSDN,
SetSelected()
可用于选择多个项目。只需对需要选择的每个项目重复调用即可。这是他们使用的示例:作为参考,this是 MSDN 文章。
According to MSDN,
SetSelected()
can be used to select multiple items. Simply repeat the call for each item that needs to be selected. This is the example they use:For reference, this is the MSDN article.
因为我的代码具有以下循环:
第一个循环遍历可用大小,第二个循环用于比较项目大小。
具有以下代码:
意味着即使项目 i 被选中,它也可以被取消选择。
解决方案:
删除
Me.lstItemSizes.SetSelected(i, False)
或包含Exit For
Because my code had the following loops:
The first loop loops through the available sizes and the second loop is used to compare the item sizes.
Having the following code:
Meant that even if item i became selected, it could also be deselected.
SOLUTION:
Remove
Me.lstItemSizes.SetSelected(i, False)
OR IncludeExit For