【问题标题】:Populating ListBox with List of <class> VB.NET用 <class> VB.NET 的列表填充 ListBox
【发布时间】:2014-08-22 14:49:51
【问题描述】:

我有一个名为 optionCode 的类:

Class optionCode
    Public description As String
    Public optCode As String
End Class

我有一个查询返回此 optionCode 类的列表:

Dim _SelectActiveOptionCodes2 = (From _OptCodes In _EntityModel.tblOptionCodes
                                Where _OptCodes.fdStatus = "A"
                                Select New optionCode With {.description = _OptCodes.fdDescription,
                                                            .optCode = _OptCodes.fdOptionCode}).ToList()

我想使用这个列表来填充一个列表框,其中描述是显示字段,选项代码是值字段。

使用时:

sortableOptionCodes = _SelectActiveOptionCodes2
sortedOptionCodes = _SelectActiveOptionCodes2
OptionCodeListBox.DataSource = sortedOptionCodes

列表框已填充,但每个条目都是“OptionCodeSearch.Form1+optionCode”

我不知道如何使用 .ValueMember 和 .DisplayMember 函数来让列表框以我想要的方式加载。

【问题讨论】:

    标签: vb.net list class listbox


    【解决方案1】:

    首先,ValueMemberDisplayMember 处理属性:Gets or sets the property to use as the actual value for the items in the System.Windows.Forms.ListControl. 所以你的类应该使用属性而不是字段(在某些情况下,字段的处理/处理方式与属性不同)。

    对于这样的类,最好覆盖 ToString,这样您就可以指定要显示的默认文本而不是类型 (OptionCodeSearch.Form1+optionCode):

    Class optionCode
        Public PROPERTY description As String
        Public PROPERTY optCode As String
    
        Public Overrides Function ToString() As String
            Return Description       ' ????
        End Function
    End Class
    

    ValueMember 通常是一个数字,但是一旦你有了List(of optionCode) ValueMemberDisplayMember 用于告诉ListBox 属性名称

    OptionCodeListBox.DisplayMember = "description"
    OptionCodeListBox.ValueMember = "optCode"
    

    并非没有,但可以将这样的原语制成一个通用的几乎可以在任何地方使用的类:

    Public Class Element(Of T)
        Public Property Name As String
    
        Public Property Value As T
    
        Friend Sub New(n As String, v As T)
            Name = n
            Value = v
        End Sub
    
        Public Overrides Function ToString() As String
            Return Name
        End Function
    
    End Class
    

    Of T 允许您在构建时定义 Value 数据类型:

    Dim el As New Element(Of Integer)(textName, intValue)
    

    您的列表将是:

    Dim myList As New List(Of Element(Of Integer))
    

    迭代列表有点麻烦,但是 VS/Intellisense 提供了提示:

    For Each El As Element(of Integer) in myList(Of Element(Of Integer))
    

    如果您在项目中大量使用其中之一,则可以将其子类化:

    Public Class optCode
        Inherits Element(Of String)
    

    现在,整个 Element(Of String) 部分已内置到您的新 optCode 类中,使其更易于键入、使用和记忆。

    这些的价值在于,您可以将相同的类用于字符串、日期时间、整数、小数等名称-值对的集合,而无需在每次需要类似的东西时编写新类。

    【讨论】:

    • 感谢您提供的解决方案以及为扩展我的编程知识所付出的额外努力。我真的很感激。
    【解决方案2】:

    编辑:此答案适用于网络表单控件。请参阅 Plutonix 对 windows 窗体控件的回答。

    我相信您正在寻找的是ListBoxDataTextFieldDataValueField 属性。

    OptionCodeListBox.DataTextField = "description"
    OptionCodeListBox.DataValueField = "optCode"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-19
      • 1970-01-01
      • 2017-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多