如何通过其索引获取ListBox项的ValueItem值

时间:2019-05-29 14:06:56

标签: vb.net winforms listbox

我有一个列表框,其中包含描述和值(描述是客户名称,valueitem是数据库中客户的ID)。在我的表单上,我有两个列表框,左边的一个具有完整的列表框客户名称列表,然后用户从该列表中进行选择,然后将他们想要与之关联的用户添加到右侧列表框中。当我要保存记录时,我想将客户ID列表存储在一个表中,以便可以识别该记录与哪些客户相关。

我的代码使用For ..循环遍历该列表。

For n = 0 To lbxCustomerList.Items.Count - 1
  U.CustomersLinkedTo += lbxCustomerList.Items(n).Value.ToString() & ","
Next

U是一个用户记录类,而字段CustomerLinkedTo是一个NVarchar(255)字段,我将用逗号分隔该列表,以列出该用户已分配给该用户的CustomerID。

问题是不允许使用lbxCustomerList.Items(n).Value。我正在使用VB.NET和Dot Net Framework V4。

我尝试将ListBox加载到ListBox.ObjectCollection变量中,然后查看是否允许我访问ValuItems,但也不允许这样做。如何获得与说明文字相反的值?

编辑**** 为了帮助那些想帮助我的好人,我在下面添加用于填充“客户”列表的代码,然后添加用于填充lbxCustomersLinkedTo的代码,该代码包含他们选择的客户:

填充用户从中选择客户名称的左侧列表,并将其添加到右侧列表中,该列表包含用户想要分配给用户的子集。

    Private Sub PopulateCustomerList()
    Dim c As New Customer()
    Dim cc As New ArrayList()

    Try
        'Clear list
        lbxCustomerList.Items.Clear()
        lbxCustomerList.DataSource = Nothing

        'Populate from DB.
        cc = c.GetCustomersForDropLists(customerTypeEnum.Active)
        lbxCustomerList.DataSource = cc
        lbxCustomerList.DisplayMember = "DisplayedText"
        lbxCustomerList.ValueMember = "ReturnedID"
        lbxCustomerList.Refresh()

    Catch ex As Exception
        PEH("PopulateCustomerList", "frmUserSetup", ex.Message)
    End Try
End Sub

将用户从“客户”列表中选择的项目添加到我感兴趣的代码的代码,该代码用于导出要添加到用户记录中的“客户ID”列表。

   Private Sub AddToList()
    Dim SelItems As New ListBox.SelectedObjectCollection(lbxCustomerList)
    SelItems = lbxCustomerList.SelectedItems

    Try

        If lbxCustomerList.SelectedItems.Count > 0 Then
            For n = 0 To SelItems.Count - 1
                lbxCustomersLinkedTo.Items.Add(SelItems(n))
            Next
            lbxCustomerList.ClearSelected()
        End If

    Catch ex As Exception
        PEH("AddToList", "frmUserSetup", ex.Message)
    End Try
End Sub

这是我正在谈论的用户表单部分的图像: Select Customers Part Of Screen

@ThePeter-感谢您对例行程序进行了一些修改,以解决问题。

这是我现在保存例程中的代码:

           'Get the Customers an officer works for into a comma separated list.
        'First remove any existing items.
        U.CustomersLinkedTo = ""

        If (lbxCustomersLinkedTo.Items.Count > 0) Then
            For n = 0 To lbxCustomersLinkedTo.Items.Count - 1
                If n = lbxCustomersLinkedTo.Items.Count - 1 Then
                    U.CustomersLinkedTo += WhatValueIsSelectedInListBoxForIndex(lbxCustomersLinkedTo, n) 'Don't add comma to last item.
                Else
                    U.CustomersLinkedTo += WhatValueIsSelectedInListBoxForIndex(lbxCustomersLinkedTo, n) & ","
                End If
            Next
        End If

这是对您的例程的稍作修改,因为它最初给出了一个错误:

   ''' <summary>
''' Use this function to get the values of an item in the list box. It works with list boxes that are bound to a DataSet, and those that have been populated via code, or manually populated via the GUI.
''' </summary>
''' <param name="lstBox">The list box you want to find the selected value in.</param>
''' <param name="iIndex">The number of the item in the ListBox that you want the value for.</param>
''' <param name="DesiredReturnValue">By default it returns the hidden return value, but you can ask for the display value if desired.</param>
''' <returns>This function returns a String value.</returns>
Public Function WhatValueIsSelectedInListBoxForIndex(ByVal lstBox As ListBox, ByVal iIndex As Integer, Optional ByVal DesiredReturnValue As SelectByMode = SelectByMode.ByReturnValue) As String
    Dim sReturn As String = ""

    Try
        If DesiredReturnValue = SelectByMode.ByReturnValue Then
            'Returns the value that is not visible, but stored in the "ValueMember" field of the bound ComboBox
            If lstBox.ValueMember.Length > 0 And lstBox.DisplayMember.Length > 0 Then
                'This is a bound listbox.
                Dim drSelectedItem As DataRowView = lstBox.Items(iIndex)
                sReturn = drSelectedItem.Item(0).ToString
            Else
                Try
                    'This listbox was populated in code with display values and return values.
                    sReturn = lstBox.Items(iIndex).ReturnedID
                Catch ex As Exception
                    PEH("WhatValueIsSelectedInListBoxForIndex - Populated Code section", "frmUserSetup", ex.Message)
                End Try
            End If
        ElseIf DesiredReturnValue = SelectByMode.ByDisplayName Then
            'Return the selected TEXT (visible in the control)
            If lstBox.ValueMember.Length > 0 And lstBox.DisplayMember.Length > 0 Then
                'This is a bound listbox.
                Dim drSelectedItem As DataRowView = lstBox.Items(iIndex)
                sReturn = drSelectedItem.Item(1).ToString
            Else
                sReturn = lstBox.Items(iIndex).ToString
            End If
        Else
            sReturn = ""
        End If
    Catch Ex As Exception

        PEH("WhatValueIsSelectedInListBoxForIndex", "frmUserSetup", Ex.Message)
        sReturn = ""
    End Try

    If sReturn Is Nothing Then sReturn = ""
    Return sReturn

End Function

我必须修改以适合我的列表框的地方是这一行:

                       'This listbox was populated in code with display values and return values.
                    sReturn = lstBox.Items(iIndex).ReturnedID

在您的帮助下,这现在可以正常工作并直接填充数据库字段。

Siv

2 个答案:

答案 0 :(得分:2)

此功能将使您能够找到列表框中所选内容的值。您可以使用它来获取所选项目的“隐藏”值或列表框中项目的显示值。

更新:我调整了功能,使其按照原始问题根据索引值起作用

Public Enum SelectByMode As Integer
   ByDisplayName = 1
   ByReturnValue = 2
End Enum

''' <summary>
''' Use this function to get the values of an item in the list box. It works with list boxes that are bound to a DataSet, and those that have been populated via code, or manually populated via the GUI.
''' </summary>
''' <param name="lstBox">The list box you want to find the selected value in.</param>
''' <param name="iIndex">The number of the item in the ListBox that you want the value for.</param>
''' <param name="DesiredReturnValue">By default it returns the hidden return value, but you can ask for the display value if desired.</param>
''' <returns>This function returns a String value.</returns>
Public Function WhatValueIsSelectedInListBoxForIndex(ByVal lstBox As ListBox, ByVal iIndex As Integer, Optional ByVal DesiredReturnValue As SelectByMode = SelectByMode.ByReturnValue) As String
    Dim sReturn As String = ""

    Try
        If DesiredReturnValue = SelectByMode.ByReturnValue Then
            'Returns the value that is not visible, but stored in the "ValueMember" field of the bound ComboBox
            If lstBox.ValueMember.Length > 0 And lstBox.DisplayMember.Length > 0 Then
                'This is a bound listbox.
                Dim drSelectedItem As DataRowView = lstBox.Items(iIndex)
                sReturn = drSelectedItem.Item(0).ToString
            Else
                Try
                    'This listbox was populated in code with display values and return values.
                    sReturn = lstBox.Items(iIndex).ReturnValue
                Catch Exp As Exception
                    '
                End Try
            End If
        ElseIf DesiredReturnValue = SelectByMode.ByDisplayName Then
            'Return the selected TEXT (visible in the control)
            If lstBox.ValueMember.Length > 0 And lstBox.DisplayMember.Length > 0 Then
                'This is a bound listbox.
                Dim drSelectedItem As DataRowView = lstBox.Items(iIndex)
                sReturn = drSelectedItem.Item(1).ToString
            Else
                sReturn = lstBox.Items(iIndex).ToString
            End If
        Else
            sReturn = ""
        End If
    Catch Exp As Exception
        'You do whatever you want to do here when an error occurs.
    End Try

    If sReturn Is Nothing Then sReturn = ""
    Return sReturn
End Function

答案 1 :(得分:1)

删除.Value部分或改用此代码

For n = 0 To lbxCustomerList.Items.Count - 1
  U.CustomersLinkedTo += lbxCustomerList.Items(n).ToString() & ","
Next

您甚至可以更轻松地使用@LarsTech所说的内容 这将不需要循环

U.CustomersLinkedTo = String.Join(", ", lbxCustomerList.Items)