为ComboBox项分配值

为ComboBox项分配值

本文介绍了为ComboBox项分配值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试为一个winform的货币制作一个下拉框(Combobox)。这是我到目前为止:





但我注意到,有一个特殊的选项为Databound版本的下拉框。所以我想知道是否可以创建类似的东西,而不诉诸于对整个字符串进行比较或在数据库中创建表。



解决方案

列表和组合框可以包含对象而不是简单的字符串。而不是在Designer中定义内容,您可以将 List(Of T)(其他)设置为 DataSource



这是非常有用的,有一个简单的 NameValuePair 类将值或代码转换为用户友好的类:

  
公共属性名称作为字符串
公共财产价值作为整数

公共Sub新(n As String,v As Integer)
Name = n
价值= v
End Sub

'这些将使用.ToString显示:
公共覆盖函数ToString()As String
返回String.Format({0} ({1}),Name,Value.ToString)
结束函数
结束类


$ b b

然后代码创建一个列表,并使用它作为组合的源代码:

 列表(NVP)
...
myAList =新列表(NVP)

myAList.Add(新NVP(ziggy,26))
myAList.Add(new NVP(able,1))
myAList.Add(new NVP(charlie,3))
myAList.Add

ComboBox1.DataSource = myAList
ComboBox1.DisplayMember =Name'属性名称显示用户
ComboBox1.ValueMember =Value'属性名称用作值

然后,使用它:

  Console.WriteLine(Selection Changed!项目:{0}价值:{1},
ComboBox1.SelectedItem.ToString,
ComboBox1.SelectedValue.ToString)

输出:



>

选择更改!项目:ziggy
选择更改!项目:charlie(3)值:3

选择已更改!项目:能(1)值:1


注意:

NameValuePair类的好处是它将数据保存在一起(容易访问),而不是作为不同数组中的单独项。



使用 DataSource 的主要好处是,您不需要复制数据。不是将 Name 值从列表复制到 Items 集合, List /



SelectedItem 返回 NVP 对象( .Items(n)),但返回 Object ,因此您需要将其强制转回 NameValuePair 以访问任何属性:

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

SelectedItem.ToString 在我们的类型上调用 ToString 方法。它可以只打印 Name 或任何你想要的。



如果动态 - 事物被添加和/或从中移除 - 那么你可能会想使用 BiningList(T)。对列表的更改将自动显示在控件中( ListBox ComboBox ,<$



如果列表是动态的 - {ziggy可能会更改为{zulu,98},那么您的项目类应实现 INotifyPropertyChanged ,以便这些更改也会自动显示在UI控件中。


I'm currently trying to make a drop box (Combobox) for currencies for a winform. 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. 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:

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
Selection Changed! Item: charlie (3) Value: 3
Selection Changed! Item: able (1) Value: 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.

The major benefit to using a DataSource, is that you do not need to make a copy of the data. Rather than copy the Name values from the list to the Items collection, the List/ComboBox uses the same ones that your code does.

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:

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

SelectedItem.ToString invokes the ToString method on our Type. It might simply print the Name or whatever you want.

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. Changes to the list will automatically appear in the control (ListBox, ComboBox, DatagridView).

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.

这篇关于为ComboBox项分配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 14:44