C#组合框使用文本和值 [英] C# ComboBox with Text and Value

查看:548
本文介绍了C#组合框使用文本和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:结果
C#的WinForms组合框与标签和值






怎样一种方式存储在一个组合框的显示值和实际值



也就是说,组合框显示:




  • 毁灭世界

  • 消防弹弓

  • 召唤邪神



但作为检索到的值是:




  • DW

  • SS

  • SC



(Visual Studio .NET中2.0请=])



其他详细信息:



我希望能够检索类似的方式选定项目的价值这样:

 串了selectedValue = combobox1.SelectedValue 

但他们需要看到在ComboBox本身更多的'用户友好'字符串



如何它已经完成:



 词典<字符串,字符串> filterItems =新词典<字符串,字符串> 
{
{毁灭世界,DW},
{火弹弓,FS},
{召唤Cthulu,SC},
};
this.options_filterby.DataSource =新的BindingSource(filterItems,NULL);
this.options_filterby.DisplayMember =键;
this.options_filterby.ValueMember =值;

现在出于某种原因,虽然DisplayMembers是精绝,对于一定量的时间程序加载后中,ValueMembers返回字典对象。

 私人无效options_filterby_SelectedIndexChanged(对象发件人,EventArgs五)
{
MessageBox.Show(options_filterby.SelectedValue.ToString());
}

这将返回字典对于前几次我改变ComboBox的选择项,但最终返回的字符串作为必要的。



的修复



在应对上述问题,解决方法是设置数据源之前将DisplayMember和ValueMember属性。我想这是一个错误。
的代码应为:

  this.options_filterby.DisplayMember =键; 
this.options_filterby.ValueMember =值;
this.options_filterby.DataSource =新的BindingSource(filterItems,NULL);


解决方案

该ComboBox.Items类型的集合存储值的对象的。因此,它可以存储任何类型的类型你的愿望。它生成的显示的从对象的ToString value()方法。要获得的真正的价值,简单地把对象到类的类型。


Possible Duplicate:
C# Winforms Combobox with Label and Value

How would one approach storing a display value and a real value in a ComboBox?

Ie, the ComboBox displays:

  • Destroy World
  • Fire Slingshot
  • Summon Cthulhu

but the values as retrieved are:

  • dw
  • ss
  • sc

(Visual Studio .NET 2.0 please =])

Additional Details:

I want to be able to retrieve the value of the selected item in a way similar to this:

string selectedValue = combobox1.SelectedValue

but they need to see a more 'user friendly' string in the combobox itself.

How it's been done:

Dictionary<string, string> filterItems = new Dictionary<string, string>
{
    {"Destroy World", "dw"},
    {"Fire Slingshot", "fs"},
    {"Summon Cthulu", "sc"},
};
this.options_filterby.DataSource = new BindingSource(filterItems, null);
this.options_filterby.DisplayMember = "Key";
this.options_filterby.ValueMember = "Value";

Now for some reason, although the DisplayMembers are absolutely fine, for some amount of time after the program loads, the ValueMembers return dictionary objects.

private void options_filterby_SelectedIndexChanged(object sender, EventArgs e)
{
    MessageBox.Show(options_filterby.SelectedValue.ToString());
}

This returns dictionaries for the first few times I change the selected item of the ComboBox, but eventually returns strings as needed.

The Fix

In response to the above problem, the fix is to set the DisplayMember and ValueMember properties before the DataSource. I presume this is a bug. The code should read:

this.options_filterby.DisplayMember = "Key";
this.options_filterby.ValueMember = "Value";
this.options_filterby.DataSource = new BindingSource(filterItems, null);

解决方案

The ComboBox.Items collection stores values of type object. So it can store any kind of type you desire. It generates the display value from the object's ToString() method. To obtain the real value, simply cast the object to the type of your class.

这篇关于C#组合框使用文本和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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