vb.net 将枚举类型的值加载到组合框中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14419196/
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
Load values of enum type into a combobox
提问by whytheq
Given the following enum:
鉴于以下枚举:
Enum enumExample
world
oblivion
holiday
End Enum
I can add its values to a list of ComboBox items like this:
我可以将其值添加到 ComboBox 项目列表中,如下所示:
combo.Items.Add(enumExample.holiday)
combo.Items.Add(enumExample.oblivion)
combo.Items.Add(enumExample.world)
Is there a shorter way?
有没有更短的方法?
回答by NinjaNye
You can use Enum.GetValues
to get a list of values for an enum then iterate the result:
您可以使用Enum.GetValues
来获取枚举的值列表,然后迭代结果:
For Each i In [Enum].GetValues(GetType(EnumExample))
combo.Items.Add(i)
Next
Or, as mentioned by @Styxxy:
或者,正如@Styxxy 所提到的:
combo.Items.AddRange([Enum].GetValues(GetType(EnumExample)))
回答by Culpepper
Why not just use:
为什么不使用:
Enum enumExample
world
oblivion
holiday
End Enum
ComboBox1.DataSource = [Enum].GetValues(GetType(enumExample))
This is what I used and it seems to have worked.
这就是我使用的,它似乎奏效了。