简体   繁体   English

为ComboBox项目分配值

[英]Assigning a value to ComboBox Items

I'm currently trying to make a drop box (Combobox) for currencies for a winform. 我目前正在尝试为Winform制作用于货币的投币箱(Combobox)。 Here's what I have so far: 这是我到目前为止的内容:

组合框输入的屏幕抓取

But I noticed that there is a special option for the Databound version of a drop down box. 但我注意到下拉框的数据绑定版本有一个特殊选项。 So I was wondering if it was possible to create something similar to this without resorting to do a comparison against the entire string or creating a table in a database. 因此,我想知道是否有可能创建类似于此的内容而无需诉诸于整个字符串的比较或在数据库中创建表。

组合框任务的屏幕截图

List and Comboboxes can contain objects rather than simply strings. 列表和组合框可以包含对象,而不仅仅是字符串。 Rather than defining the contents in the Designer, you can set a List(Of T) (among others) as the DataSource . 您可以将List(Of T) (以及其他设置)设置为DataSource ,而不是在Designer中定义内容。 This allows you to display one thing but fetch a different thing such as a value back. 这使您可以显示一件事,但获取另一件事,例如返回值。

This is so useful, it is not uncommon to have a simple NameValuePair class to translate a value or code into something user-friendly: 这是如此有用,拥有一个简单的NameValuePair类将值或代码转换为用户友好的东西并不少见:

Public Class NVP
    Public Property Name As String
    Public Property Value As Integer

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

    ' what these will display using .ToString:
    Public Overrides Function ToString() As String
        Return String.Format("{0} ({1})", Name, Value.ToString)
    End Function
End Class

Then the code to create a list of these and use it as the source for the combo: 然后,代码创建这些列表并将其用作组合的源:

Private myAList As List(Of NVP)
...
myAList = New List(Of NVP)

myAList.Add(New NVP("ziggy", 26))
myAList.Add(New NVP("able", 1))
myAList.Add(New NVP("charlie", 3))
myAList.Add(New NVP("echo", 5))

ComboBox1.DataSource = myAList
ComboBox1.DisplayMember = "Name"    ' property name to show the user
ComboBox1.ValueMember = "Value"     ' property name to use as the value

Then, using it: 然后,使用它:

Console.WriteLine("Selection Changed! Item: {0}  Value: {1}",
                   ComboBox1.SelectedItem.ToString,
                   ComboBox1.SelectedValue.ToString)

Output: 输出:

Selection Changed! 选择已更改! Item: ziggy (26) Value: 26 项目:齐吉(26)价值:26
Selection Changed! 选择已更改! Item: charlie (3) Value: 3 项目:查理(3)价值:3
Selection Changed! 选择已更改! Item: able (1) Value: 1 项目:能够(1)价值:1

Notes: 笔记:
The benefit to the NameValuePair class is that it keep the data together (and easily accessed) rather then as separate items in different arrays. NameValuePair类的好处在于,它将数据保持在一起(并易于访问),而不是将它们作为不同数组中的单独项。

The major benefit to using a DataSource , is that you do not need to make a copy of the data. 使用DataSource的主要好处是您无需复制数据。 Rather than copy the Name values from the list to the Items collection, the List/ComboBox uses the same ones that your code does. List/ComboBox不会将Name值从列表复制到Items集合,而是使用代码相同的值

SelectedItem returns an NVP object (as does .Items(n) ) but they are returned as Object so you need to cast it back to NameValuePair to access any properties: SelectedItem返回一个NVP对象( .Items(n) ),但是它们作为Object返回,因此您需要将其NameValuePair转换回NameValuePair以访问任何属性:

thisItem = CType(ComboBox1.SelectedItem, NVP)
Console.WriteLine(thisItem.Name)

SelectedItem.ToString invokes the ToString method on our Type. SelectedItem.ToString在我们的Type上调用ToString方法。 It might simply print the Name or whatever you want. 它可能只打印Name或您想要的任何内容。

If the list is dynamic - things get added and/or removed from it - then you will probably want to use a BiningList(of T) instead. 如果列表是动态的-事物被添加和/或删除-那么您可能会想使用BiningList(of T) Changes to the list will automatically appear in the control ( ListBox , ComboBox , DatagridView ). 列表所做的更改将自动显示在控件( ListBoxComboBoxDatagridView )中。

If the list items are dynamic - {"ziggy", 26} might be changed to {"zulu", 98}, then your item class should implement INotifyPropertyChanged so those changes also automatically show in the UI control. 如果列表是动态的-{“ ziggy”,26}可能会更改为{“ zulu”,98},则您的项类应实现INotifyPropertyChanged以便这些更改也自动显示在UI控件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM