多选列表框

发布于 2024-10-29 07:34:36 字数 933 浏览 2 评论 0原文

我的表单上有一个列表框,它可以很好地满足我想要做的事情。

我想要编辑表单上的项目,这意味着填充列表框,然后选择相关项目。

我的列表框包含项目尺寸列表,我想选择属于正在编辑的项目的尺寸。

请有人给我一些指点。

我尝试了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

落花随流水 2024-11-05 07:34:36

您是否将选择模式设置为多重?

您需要指定它才能允许多项选择。

然后你可以这样做:

Dim i as Integer=0

For i=0 To Me.listBox.SelectedItems.Count -1
  'display the listbox value
next i

这是一个屏幕截图:

在此处输入图像描述

在列表框中设置属性后,然后调用 setselected基于您想要选择的值。

me.lstItemSizes.SetSelected(3,true)
me.lstItemSizes.SetSelected(4,true)
me.lstItemSizes.SetSelected(9,true)

在这里您可以添加 20 个数字,并且只选择偶数。

    Dim i As Integer

            'load the list with 20 numbers
            For i = 0 To 20
                Me.ListBox1.Items.Add(i)
            Next

            'now use setselected
            'assume only even are selected
            For i = 0 To 20
                If i Mod 2 = 0 Then
                    Me.ListBox1.SetSelected(i, True)
                End If
            Next

第三次编辑

看看你循环的方式,假设我创建一个整数列表,我的 vb.net 生锈了,我主要用 C# 开发。但假设您这样做了:

     Dim l As New List(Of Integer)

            l.Add(2)
            l.Add(6)
            l.Add(20)

您的列表中只有三个项目,因此首先根据列表中的项目循环,然后在列表框中的项目中,反之亦然。看一下:

 Dim i As Integer
        Dim l As New List(Of Integer)

        l.Add(2)
        l.Add(6)
        l.Add(20)

        'load the list with 20 numbers
        For i = 0 To 20
            Me.ListBox1.Items.Add(i)
        Next

        Dim lCount As Integer = 0

        For lCount = 0 To l.Count - 1
            For i = 0 To 20
                If i = l.Item(lCount) Then
                    Me.ListBox1.SetSelected(i, True)
                    Exit For
                End If
            Next
        Next

在代码中,我的 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:

Dim i as Integer=0

For i=0 To Me.listBox.SelectedItems.Count -1
  'display the listbox value
next i

Here is a screen shot:

enter image description here

After you set the property on the listbox then call setselected based on the values you want selected.

me.lstItemSizes.SetSelected(3,true)
me.lstItemSizes.SetSelected(4,true)
me.lstItemSizes.SetSelected(9,true)

Here you can add 20 numbers and only select the even.

    Dim i As Integer

            'load the list with 20 numbers
            For i = 0 To 20
                Me.ListBox1.Items.Add(i)
            Next

            'now use setselected
            'assume only even are selected
            For i = 0 To 20
                If i Mod 2 = 0 Then
                    Me.ListBox1.SetSelected(i, True)
                End If
            Next

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:

     Dim l As New List(Of Integer)

            l.Add(2)
            l.Add(6)
            l.Add(20)

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:

 Dim i As Integer
        Dim l As New List(Of Integer)

        l.Add(2)
        l.Add(6)
        l.Add(20)

        'load the list with 20 numbers
        For i = 0 To 20
            Me.ListBox1.Items.Add(i)
        Next

        Dim lCount As Integer = 0

        For lCount = 0 To l.Count - 1
            For i = 0 To 20
                If i = l.Item(lCount) Then
                    Me.ListBox1.SetSelected(i, True)
                    Exit For
                End If
            Next
        Next

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

enter image description here

终遇你 2024-11-05 07:34:36

您必须更改 ListBox.SelectionMode属性以启用多重选择。
可能的值由 SelectionModeenum,如下:

:无法选择任何项目
一个:只能选择一项
MultiSimple:可以选择多个项目
MultiExtended:可以选择多个项目,用户可以使用ShiftCtrl和箭头键进行选择

因此,您只需将以下行添加到已有的代码中:

' Change the selection mode (you could also use MultiExtended here)
lstItemSizes.SelectionMode = SelectionMode.MultiSimple;

' Select any items of your choice
lstItemSizes.SetSelected(1, True)
lstItemSizes.SetSelected(3, True)
lstItemSizes.SetSelected(8, True)

或者,您可以在设计时设置 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:

None: No items can be selected
One: Only one item can be selected
MultiSimple: Multiple items can be selected
MultiExtended: Multiple items can be selected, and the user can use the Shift, Ctrl, and arrow keys to make selections

So, you simply need to add the following line to the code you already have:

' Change the selection mode (you could also use MultiExtended here)
lstItemSizes.SelectionMode = SelectionMode.MultiSimple;

' Select any items of your choice
lstItemSizes.SetSelected(1, True)
lstItemSizes.SetSelected(3, True)
lstItemSizes.SetSelected(8, True)

Alternatively, you can set the SelectionMode property at design time, instead of doing it with code.

夏の忆 2024-11-05 07:34:36

根据 MSDN,SetSelected() 可用于选择多个项目。只需对需要选择的每个项目重复调用即可。这是他们使用的示例:

' Select three items from the ListBox.
listBox1.SetSelected(1, True)
listBox1.SetSelected(3, True)
listBox1.SetSelected(5, True)

作为参考,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:

' Select three items from the ListBox.
listBox1.SetSelected(1, True)
listBox1.SetSelected(3, True)
listBox1.SetSelected(5, True)

For reference, this is the MSDN article.

指尖微凉心微凉 2024-11-05 07:34:36

因为我的代码具有以下循环:

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

第一个循环遍历可用大小,第二个循环用于比较项目大小。

具有以下代码:

Else
 Me.lstItemSizes.SetSelected(i, False)
End If

意味着即使项目 i 被选中,它也可以被取消选择。

解决方案:
删除 Me.lstItemSizes.SetSelected(i, False) 或包含 Exit For

Because my code had the following loops:

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

The first loop loops through the available sizes and the second loop is used to compare the item sizes.

Having the following code:

Else
 Me.lstItemSizes.SetSelected(i, False)
End If

Meant that even if item i became selected, it could also be deselected.

SOLUTION:
Remove Me.lstItemSizes.SetSelected(i, False) OR Include Exit For

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文