多选列表框 [英] Multi Select List Box

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

问题描述

我在表单上有一个列表框,它可以很好地满足我的需求.

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.

请有人给我一些指示.

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

I tried me.lstItemSizes.SetSelected(i,true) but this only works for a single item.

任何帮助将不胜感激.

我的代码:

    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

推荐答案

您是否将selectionmode设置为multi?

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

以下是屏幕截图:

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

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)

在这里您可以添加20个数字,而只能选择偶数.

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

第3次修改

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

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

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

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.

在我的for循环中注意,一旦找到列表中的项目,我就不再需要循环,所以我exit for.这样可以确保我不会过期所需的循环次数.找到该项目后,离开并返回到列表对象计数的计数.

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

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

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