枚举绑定到一个WinForms组合框,然后将其设置 [英] Binding an enum to a WinForms combo box, and then setting it

查看:320
本文介绍了枚举绑定到一个WinForms组合框,然后将其设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多人都回答了如何将枚举绑定到的WinForms组合框的问题。它是这样的:

a lot of people have answered the question of how to bind an enum to a combo box in WinForms. Its like this:

comboBox1.DataSource = Enum.GetValues(typeof(MyEnum));

但是,这是pretty的,而不能设置实际值显示无用的。

But that is pretty useless without being able to set the actual value to display.

我曾尝试:

comboBox1.SelectedItem = MyEnum.Something; // Does not work. SelectedItem remains null

我也曾尝试:

I have also tried:

comboBox1.SelectedIndex = Convert.ToInt32(MyEnum.Something); // ArgumentOutOfRangeException, SelectedIndex remains -1

没有人有任何想法如何做到这一点?

Does anyone have any ideas how to do this?

推荐答案

枚举

public enum Status { Active = 0, Canceled = 3 }; 

设置下拉值从它

Setting the drop down values from it

cbStatus.DataSource = Enum.GetValues(typeof(Status));

获取枚举从所选择的项目

Getting the enum from the selected item

Status status; 
Enum.TryParse<Status>(cbStatus.SelectedValue.ToString(), out status); 

这篇关于枚举绑定到一个WinForms组合框,然后将其设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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