在没有 DataSource 的情况下在 ComboBox 上设置 DisplayMember 和 ValueMember

Set DisplayMember and ValueMember on ComboBox without DataSource

提问人:genespos 提问时间:7/5/2016 最后编辑:Ňɏssa Pøngjǣrdenlarpgenespos 更新时间:7/19/2019 访问量:20628

问:

我想要一个和一个只有 4 个值的 a 和 a,它们总是相同的。DisplayMemberValueMemberComboBox

是否可以不使用 as 和不创建类?DataTableDataSource

我想要这样的东西:

ValueMember= "Fixed"  
DisplayMember= "Specific and unique number"

ValueMember= "Multiple"  
DisplayMember= "Multiple and different numbers"

ValueMember= "Repeated"  
DisplayMember= "One number repeated x times"
.net vb.net 组合框

评论

0赞 Ňɏssa Pøngjǣrdenlarp 7/5/2016
这些用于映射对象的属性。如果你的有“价值观”,就没有什么可映射的。里面到底有什么?
0赞 genespos 7/5/2016
@Plutonix 我需要值(字符串),但我想显示另一个带有解释的字符串
0赞 Ňɏssa Pøngjǣrdenlarp 7/5/2016
仍然不清楚,但也许您可以使用格式事件
0赞 genespos 7/5/2016
@Plutonix我编辑了我的问题。希望现在清楚了

答:

0赞 bstarr 7/5/2016 #1

cbActive 是一个组合框

Private Sub LoadActiveCB()

    Dim _Active As New List(Of ActiveCB)

    _Active.Add(New ActiveCB With {.Name = "Fixed", .ID = 1})
    _Active.Add(New ActiveCB With {.Name = "Multiple", .ID = 2})
    _Active.Add(New ActiveCB With {.Name = "Repeated", .ID = 3})

    cbActive.DataSource = _Active
    cbActive.DisplayMember = "Name"
    cbActive.ValueMember = "ID"

End Sub

Class ActiveCB
    Property Name As String
    Property ID As Byte
End Class

你必须稍微调整一下,改变显示和值成员,但我相信这可能是你想要的......?

评论

0赞 genespos 7/5/2016
什么?我不想使用自定义类。ActiveCB
0赞 Ňɏssa Pøngjǣrdenlarp 7/5/2016
@genespos只需删除它,它将是一个匿名类型。你也可以从修饰的枚举中引导它
0赞 bstarr 7/5/2016
哦,对不起,我忘了......我的错。我不明白你对这个的应用,因为它只是像“类 ActiveCB 属性名称作为字符串属性 ID 作为字节结束类”
0赞 genespos 7/5/2016
@Plutonix 我不明白你的意思:如何在不设置新类的情况下使用此代码?
0赞 bstarr 7/5/2016 #2

我想我现在明白了......也许是这样的:

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim comboSource As New Dictionary(Of String, String)()
        comboSource.Add("1", "Sunday")
        comboSource.Add("2", "Monday")
        comboSource.Add("3", "Tuesday")
        comboSource.Add("4", "Wednesday")
        comboSource.Add("5", "Thursday")
        comboSource.Add("6", "Friday")
        comboSource.Add("7", "Saturday")
        ComboBox1.DataSource = New BindingSource(comboSource, Nothing)
        ComboBox1.DisplayMember = "Value"
        ComboBox1.ValueMember = "Key"
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim key As String = DirectCast(ComboBox1.SelectedItem, KeyValuePair(Of String, String)).Key
        Dim value As String = DirectCast(ComboBox1.SelectedItem, KeyValuePair(Of String, String)).Value
        MessageBox.Show(key & "   " & value)
    End Sub
End Class

信用:http://net-informations.com/q/faq/combovalue.html

3赞 Fabio 7/5/2016 #3

使用某个类是一种解决方案,但因为您不想使用自定义类 - 使用内置类型。

Dim values As New List(Of Tuple(Of String, String))()
values.Add(New Tuple("Fixed", "Specific and unique number"))
values.Add(New Tuple("Multiple", "Multiple and different numbers"))
values.Add(New Tuple("Repeated", "One number repeated x times"))
'and so on...

combobox.ValueMember = "Item1"
combobox.DisplayMember = "Item2"
combobox.DataSource = values

元组(T1、T2)类

但为了可读性,我建议使用自定义类。
或者只是在重用自定义类的情况下比
Tuple

10赞 Ňɏssa Pøngjǣrdenlarp 7/5/2016 #4

从根本上说,你不能做你想做的事:

ValueMember= "Fixed"  
DisplayMember= "Specific and unique number"

Value-并且不用于指定文本值,而是用于指示其他内容(如类)中的属性名称DisplayMember


不使用(标题)与不使用类(问题文本)不同。创建类还有其他替代方法:DataSource

现有 NET 类型

您可以使用现有的 NET 类将值与名称链接:KeyValuePair

cbox.Items.Add(New KeyValuePair(Of String, String)("Specific", 
         "Specific and unique number"))
cbox.Items.Add(New KeyValuePair(Of String, String)("Multiple", 
         "Multiple and different numbers"))
cbox.Items.Add(New KeyValuePair(Of String, String)("Repeated", 
         "One number repeated x times"))

cbox.ValueMember = "Key"
cbox.DisplayMember = "Value"

没有 DataSource - 数据位于 items 集合中。正如另一个答案所解释的那样Tuple


匿名类型

使用一个字符串作为另一个字符串的键是很奇怪的。通常,在代码中,您会希望一些不太容易出现拼写错误的东西。在某处键入“Fized”会破坏您的代码。An 更有意义:Enum

Private Enum ValueStyle
    Specific = 0
    Multiple = 1
    Repeated = 2
End Enum

现在,您可以创建一个链接用户描述和常量:ListEnum

' fuller text descr of the enum for the user
Dim descr As String() = {"Specific and unique number",
                         "Multiple and different numbers",
                         "One number repeated x times"}
' get enum values into an array of ValueStyle
Dim values = [Enum].GetValues(GetType(ValueStyle)).Cast(Of ValueStyle).ToArray

' create a List of anon objects from the descr() and values()
Dim lst = values.Select( Function (q) New With
                       {.Value = q, .Name = descr (q)}
                    ).ToList()
    
cboPicker.ValueMember = "Value"
cboPicker.DisplayMember = "Name"
cboPicker.DataSource = lst

这将创建一个 Anonymous Type(一个没有类的对象),其 Name 和 Value 属性映射到 Enum 和 description 数组。如果值不是连续的(例如 {8, 65, 99}),则必须以不同的方式构建列表。Enum

这将创建 Anonymous Type 对象的临时集合,并将其指定为 DataSource。您将无法访问其他方法中的 and 属性,因为匿名类型无法传递给其他方法。但是用户将看到所需的文本,并且 NET/VB 将提供该枚举的值作为 .使用更改的事件:NameValueSelectedValueSelectedValue

' name user sees == cboPicker.Text
' value == cboPicker.SelectedValue boxed as Object

Dim userChoice As ValueStyle = CType(cboPicker.SelectedValue, ValueStyle)
If userChoice = ValueStyle.Specific Then
    '...
ElseIf userChoice = ValueStyle.Repeated Then
    '...
End If

请注意,该代码不是将“Fixed”测试为字符串,而是使用枚举,但仍然具有可读性。

MSDN:匿名类型 (Visual Basic)


这些符合不需要新类的标准,但请考虑:

Friend Class NameValuePair
    Public Property Name As String
    Public Property Value As Int32

    Public Sub New(n As String, v As Int32)
        Name = n
        Value = v
    End Sub

    Public Overrides Function ToString() As String
        Return Name
    End Function

End Class

该类非常简单,在将 any 与 any 相关联时几乎可以无限重用。它可以在任意数量的项目中与任意数量的基于列表的控件一起使用。创建和使用它们列表的代码比使用其他方法更简单。NameValue

评论

1赞 genespos 7/5/2016
非常感谢您的详细回答。我会研究它(我仍然无法理解你所展示的一切)
0赞 Alex Alvarez 7/19/2019 #5

给定一个指向 VB 中数据库实体的匿名类型的 IList,我使用它来构建一个匿名列表,其中一个成员指向 db 实体对象 (PO),另一个成员指向实体中值的复合 (Display):

Dim aList = _BO_Data.Lines
       .Select(Function(PO) ew With {.ACBond = PO, .Display = (PO.Product.Code_item & " - " & PO.Product.Descr)}).ToList()

Combobox_Code_Product.DataSource = aList
Combobox_Code_Product.DisplayMember = "Display"
Combobox_Code_Product.ValueMember = "ACBond"

然后组合框 SelectedItem 是 ACBond 对象,我在列表中看到的是代码和产品描述的混合。