vb.net 多选列表框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5488211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:23:15  来源:igfitidea点击:

Multi Select List Box

vb.netwinformslistboxmulti-select

提问by Richard.Gale

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.

我试过了,me.lstItemSizes.SetSelected(i,true)但这仅适用于单个项目。

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

回答by JonH

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.

在列表框上设置属性后,根据要选择的值调用 setselected。

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.

在这里您可以添加 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

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:

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

     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.

在代码中,我的 l 是一个只有 3 个项目的列表:2、6 和 20。我将这些项目添加到 l,它只是一个列表对象。所以现在我必须循环使用这 3 个数字并与我的列表框进行比较。你有它的相反,你在你的列表框上循环,然后考虑到列表对象。

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.

请注意,在我的 for 循环中,一旦找到我列表中的项目,我就不再需要循环,因此我exit for. 这确保我不会逾期所需的循环量。找到该项目后,返回列表对象计数的计数。

After running my code here is the result

在这里运行我的代码后是结果

enter image description here

在此处输入图片说明

回答by Cody Gray

You have to change the ListBox.SelectionModepropertyin order to enable multiple-selection.
The possible values are given by the SelectionModeenum, as follows:

您必须更改该属性才能启用多选。 可能的值由enum给出,如下所示:ListBox.SelectionMode
SelectionMode

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

None: 没有项可以选择
One: 只能选择一项
MultiSimple: 可以选择多项
MultiExtended:可以选择多项,用户可以使用ShiftCtrl、 和箭头键进行选择


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 SelectionModeproperty at design time, instead of doing it with code.

或者,您可以SelectionMode在设计时设置属性,而不是使用代码来设置。

回答by Ender

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:

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

' 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.

作为参考,这是 MSDN 文章

回答by Richard.Gale

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.

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

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

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