VB.Net Combobox 显示引用值的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15173903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.Net Combobox to display text which refers to a value
提问by user2126509
I would like to display a number of items in a combobox and when the user selects on, instead of the text being returned a specific value referring to that text is returned. Any help with this would be much appreciated.
我想在组合框中显示许多项目,当用户选择时,而不是返回文本,而是返回引用该文本的特定值。对此的任何帮助将不胜感激。
回答by Neolisk
This is a typical Key,Valuebinding. I will use a Dictionary, as an example. So assuming you have populated your Dictionary with proper values, the code can look like this:
这是典型的Key,Value绑定。我将使用Dictionary, 作为示例。因此,假设您已经用正确的值填充了 Dictionary,代码可能如下所示:
ComboBox1.DataSource = yourDictionary.ToList
ComboBox1.DisplayMember = "Value"
ComboBox1.ValueMember = "Key"
You can then pick the value using ComboBox1.SelectedValueproperty.
然后,您可以使用ComboBox1.SelectedValue属性选择值。

