C# 如何从 WinForms 中的 ComboBox 获取枚举值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1293118/
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
C# How to get the Enum value from ComboBox in WinForms?
提问by user146584
I am binding a Dictionary to a ComboBox which keeps the Enum values.
我将字典绑定到保留枚举值的 ComboBox。
To retrieve the selected value I used:comboBox1.SelectedItem
which returns a dimensional value of [0,Permanent]
.
要检索我使用的选定值:comboBox1.SelectedItem
它返回[0,Permanent]
.
I just want to retrieve Permanent
and then convert it back to Enum.
我只想检索Permanent
然后将其转换回 Enum。
Something like:Employee.JobType = Enum.Parse(JobType, comboBox1.SelectedItem)
就像是:Employee.JobType = Enum.Parse(JobType, comboBox1.SelectedItem)
How can I achieve this?
我怎样才能做到这一点?
采纳答案by LukeH
Either:
任何一个:
Employee.JobType = (JobType)Enum.Parse(typeof(JobType), comboBox1.SelectedValue);
Or:
或者:
Employee.JobType = (JobType)Enum.Parse(typeof(JobType), comboBox1.SelectedText);
回答by Fredrik M?rk
This would do the trick, I think:
我认为这可以解决问题:
string[] parts = comboBox1.SelectedItem.Split(
new char[] { ',', '[', ']' },
StringSplitOptions.RemoveEmptyEntries);
Employee.JobType = (JobType)Enum.Parse(typeof(JobType), parts[1].Trim()));
First, split up the string using comma and the square brackets, and have the method remove any empty elements. This should leave you with an array containing the number and the text. Use the text part for enum parsing.
首先,使用逗号和方括号拆分字符串,并让该方法删除所有空元素。这应该为您留下一个包含数字和文本的数组。使用文本部分进行枚举解析。
Note that you need to pass the Type
object for the enum into the Parse
method, and then you need to cast the result, since the return type of Parse
is object
.
请注意,您需要Type
将枚举的对象传递给Parse
方法,然后您需要对结果进行强制转换,因为 的返回类型Parse
是object
。
回答by maciejkow
If items source for combo box is a dictionary, SelectedItem is of type: KeyValuePair<[type of key], JobType>
如果组合框的项目源是字典,则 SelectedItem 的类型为:KeyValuePair<[key of key], JobType>
You can access your enum value by casting SelectedItem and accessing Value property.
您可以通过转换 SelectedItem 并访问 Value 属性来访问您的枚举值。
var selectedItem = (KeyValuePair<[type of key], JobType>) comboBox1.SelectedItem;
var jobType = selectedItem.Value;
回答by vit
Employee.JobType = (JobTypeEnum)Enum.Parse(typeof(JobTypeEnum), comboBox1.SelectedValue);
回答by Bhaskar
See this -- http://www.fmsinc.com/free/NewTips/NET/NETtip4.asp
看到这个——http://www.fmsinc.com/free/NewTips/NET/NETtip4.asp
PeopleNames people = (PeopleNames)Enum.Parse(ComboBox1.SelectedValue, PeopleNames)
Data Binding:
数据绑定:
ComboBox1.DataSource = System.Enum.GetValues(typeof(PeopleNames))
回答by Muckers Mate
I had the same problem - (WPF) where my combo contained an enumeration in keyvalue pairs.
我有同样的问题 - (WPF)我的组合包含键值对中的枚举。
The ONLY way I could get the enumeration out was by
我能得到枚举的唯一方法是通过
KeyValuePair<string,string> selectedPair = (KeyValuePair<string,string>)(cmbApplications.SelectedItem);
ProTraceLicence.Products chosenProduct = (ProTraceLicence.Products)Enum.Parse(typeof(ProTraceLicence.Products), selectedPair.Key);
Hope this helps someone. Can't believe it's so difficult
希望这可以帮助某人。不敢相信这么难