在没有DataSource的ComboBox上设置DisplayMember和ValueMember [英] Set DisplayMember and ValueMember on ComboBox without DataSource

查看:258
本文介绍了在没有DataSource的ComboBox上设置DisplayMember和ValueMember的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 ComboBox 上拥有 DisplayMember ValueMember code>只有4个值,它们总是相同的。

I would like to have a DisplayMember and a ValueMember on a ComboBox which has only 4 values and they are always the same.

可以不使用 DataTable DataSource 并且不创建类?

Is it possible without using a DataTable as DataSourceand without creating a class?

我想有类似:

ValueMember =固定

DisplayMember =特定和唯一编号

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

ValueMember =多个

DisplayMember =多个不同的数字

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

ValueMember =

DisplayMember =一个数字重复x次

ValueMember= "Repeated"
DisplayMember= "One number repeated x times"

推荐答案

不使用 DataSource (title)与不使用类(问题文本)不同。有创建类的替代方法:

Without using a DataSource (title) is not the same as not using a class (question text). There are alternatives to creating a class:

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

You could use the existing NET KeyValuePair class to link a value with a name:

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

There is no DataSource - the data is in the items collection. There is also Tuple as explained in another answer

使用一个字符串作为另一个字符串的键有点奇怪。通常在代码中,你会想要的东西不容易错误的错别字。在某处键入Fized打破了代码。枚举更有意义:

Using one string as the key for another string is a bit odd. Typically in code you would want something less prone to errors from typos. Typing "Fized" somewhere breaks your code. An enum makes much more sense:

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

现在,您可以创建列表,其中链接用户的描述和枚举常量:

Now, you can create a List which links a description for the user and the Enum constants:

' 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) q.New With
                       {.Value = q, .Name = descr (q)}
                    ).ToList()

cboPicker.ValueMember = "Value"
cboPicker.DisplayMember = "Name"
cboPicker.DataSource = lst

这将创建一个匿名类型 - 一个没有类的对象,名称和值属性映射到枚举和描述数组。如果枚举值不是顺序的(例如{8,65,99}),则列表必须以不同的方式构建。

This creates an Anonymous Type - an object without a class - with a Name and Value property mapped to the Enum and description array. If the Enum values are not sequential (like {8, 65, 99}), the list would have to be built differently.

这将创建匿名类型对象并将其指定为DataSource。你不能访问其他方法中的 Name Value 属性,因为匿名类型,无法传递到其他方法。但是用户将看到所需的文本,NET / VB将提供作为 SelectedValue 的枚举的值。使用 SelectedValue 更改事件:

This creates a temporary collection of Anonymous Type objects and assignes it as the DataSource. You wont be able to access the Name and Value properties in other methods because anonymous type those cant be passed to other methods. But the user will see the desired text and NET/VB will provide the value as that enum as the SelectedValue. Using the SelectedValue changed event:

' 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作为字符串,但仍然每一位都是可读的。

Notice that rather than testing "Fixed" as a string, the code uses the enum but is still every bit as readable.

MSDN: 匿名类型(Visual Basic)

MSDN: Anonymous Types (Visual Basic)

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

Those fit the criteria of not needing a new class, but consider:

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

类非常简单无限可重用于将任何 Name 与任何 Value 关联。它可以用于任何数量的基于列表的控件,在任何数量的项目。创建和使用列表的代码比使用其他方法更简单。

The class is very simple and is almost infinitely reusable in associating any Name with any Value. It could be used with any number of list based controls, in any number of projects. The code to create and use a list of them is simpler than using the other methods.

这篇关于在没有DataSource的ComboBox上设置DisplayMember和ValueMember的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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