使用列表< class>填充ListBox。 VB.NET [英] Populating ListBox with List of <class> VB.NET
问题描述
我有一个名为optionCode的类:
类选项代码
公共描述As String
public optCode as String
结束类
我有一个查询返回此optionCode类的列表:
Dim _SelectActiveOptionCodes2 =(From _OptCodes In _EntityModel.tblOptionCodes
其中_OptCodes.fdStatus =A
选择新选项代码使用{.description = _OptCodes.fdDescription,
.optCode = _OptCodes.fdOptionCode})。ToList()
我想使用此列表来填充列表框,其中描述是显示字段,选项代码是值字段。
:
sortableOptionCodes = _SelectActiveOptionCodes2
sortedOptionCodes = _SelectActiveOptionCodes2
OptionCodeListBox.DataSource = sortedOptionCodes
列表框填充,但每个条目都是OptionCodeSearch.Form1 + optionCode
我无法弄清楚如何使用.ValueMember和.DisplayMember函数来获取列表框加载我想要的方式。
首先, ValueMember
和 DisplayMember
工作关闭属性:获取或设置
所以你的类应该使用属性而不是字段(有一些条件,其中字段的处理/处理不同于属性)。
对于这样的类,最好重写ToString,以便指定要显示的默认文本,而不是Type( OptionCodeSearch.Form1 + optionCode
):
class optionCode
public PROPERTY description As String
public PROPERTY optCode As String
公共覆盖函数ToString()As String
返回描述'????
结束函数
结束类
ValueMember
通常是数字,但是一旦你有
用于告诉 List(of optionCode)
ValueMember
code> DisplayMember ListBox
属性名称:
OptionCodeListBox.DisplayMember =description
OptionCodeListBox.ValueMember =optCode
不是什么都可以,但像这样的原语可以做成一个通用的用法 - 几乎任何类:
公共类元素(T)
公共属性名称作为字符串
公共属性值As T
Friend Sub New(n As String,v As T)
Name = n
Value = v
End Sub
公共覆盖函数ToString()As String
返回名称
结束函数
结束类
b $ b
可以在构建时定义Value数据类型:
Dim el As New Element(Of Integer)(textName,intValue)
您的列表将是:
Dim myList作为新列表(对于整数)元素
迭代列表有点麻烦,但VS / Intellisense提供了提示:
对于myList中的每个元素(整数)元素
如果你在项目中使用了很多,你可以将其子类化:
Public class optCode
Inherits Element(Of String)
现在, code>元素(Of String)部分内置在新的 optCode
类中,使其更容易输入,使用和记住。 / p>
这些的价值在于,你可以为string,datetime,integer,decimal等的Name-Value对的集合使用相同的类,而不必每次都编译新类你需要类似的东西。
I have a class called optionCode:
Class optionCode
Public description As String
Public optCode As String
End Class
I have a query the returns a list of this optionCode class:
Dim _SelectActiveOptionCodes2 = (From _OptCodes In _EntityModel.tblOptionCodes
Where _OptCodes.fdStatus = "A"
Select New optionCode With {.description = _OptCodes.fdDescription,
.optCode = _OptCodes.fdOptionCode}).ToList()
I want to use this list to populate a listbox where the description is the display field and the option code is the value field.
When using:
sortableOptionCodes = _SelectActiveOptionCodes2
sortedOptionCodes = _SelectActiveOptionCodes2
OptionCodeListBox.DataSource = sortedOptionCodes
The listbox populates, but every entry is "OptionCodeSearch.Form1+optionCode"
I can't figure out how to use the .ValueMember and .DisplayMember functions to get the listbox to load the way I want.
First, ValueMember
and DisplayMember
work off properties: Gets or sets the property to use as the actual value for the items in the System.Windows.Forms.ListControl.
So your class should use Properties not Fields (there are conditions where Fields are treated/handled differently than Properties).
With such classes, it good idea to override ToString so you can specify the default text to display instead of the Type (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
is typically a numeric, but otherwise once you have the List(of optionCode)
ValueMember
and DisplayMember
are used to tell the ListBox
the Property Names:
OptionCodeListBox.DisplayMember = "description"
OptionCodeListBox.ValueMember = "optCode"
Not for nothing, but primitives like that can be made into a generic use-almost-anywhere class:
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
allows you to define the Value data type when you build it:
Dim el As New Element(Of Integer)(textName, intValue)
Your list would then be:
Dim myList As New List(Of Element(Of Integer))
It is a little cumbersome to iterate lists, but VS/Intellisense provide hints:
For Each El As Element(of Integer) in myList(Of Element(Of Integer))
If you are using one of these a lot in a project, you can subclass it:
Public Class optCode
Inherits Element(Of String)
Now, the whole Element(Of String)
part is built into your new optCode
class, making it easier to type, use and remember.
The value of these is that you can use the same class for a collection of Name-Value pairs of string, datetime, integer, decimal etc without coding a new class each time you need something like it.
这篇关于使用列表< class>填充ListBox。 VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!