我们常看到VS.net中可以将一些枚举型的值放到下拉框中显示,就不禁自问:如何实现的?
那么加到下拉框也就很简单了。
1
using System;
2
3
public class GetNamesTest
4
{
5
enum Colors
6
{
7
Red, Green, Blue, Yellow
8
};
9
enum Styles
10
{
11
Plaid, Striped, Tartan, Corduroy
12
};
13
14
public static void Main()
15
{
16
Console.WriteLine("The values of the Colors Enum are:");
17
foreach(string s in Enum.GetNames(typeof(Colors)))
18
Console.WriteLine(s);
19
Console.WriteLine();
20
Console.WriteLine("The values of the Styles Enum are:");
21
foreach(string s in Enum.GetNames(typeof(Styles)))
22
Console.WriteLine(s);
23
}
24
using System;2

3
public class GetNamesTest 4
{5
enum Colors 6
{ 7
Red, Green, Blue, Yellow 8
};9
enum Styles 10
{ 11
Plaid, Striped, Tartan, Corduroy 12
};13

14
public static void Main() 15
{16
Console.WriteLine("The values of the Colors Enum are:");17
foreach(string s in Enum.GetNames(typeof(Colors)))18
Console.WriteLine(s);19
Console.WriteLine();20
Console.WriteLine("The values of the Styles Enum are:");21
foreach(string s in Enum.GetNames(typeof(Styles)))22
Console.WriteLine(s);23
}24

那么加到下拉框也就很简单了。
1
string [] names = Enum.GetNames(typeof(DockStyle));
2
comboBox1.Items.Clear();
3
foreach(string name in names)
4
{
5
this.comboBox1.Items.Add(name);
6
}
string [] names = Enum.GetNames(typeof(DockStyle));2
comboBox1.Items.Clear();3
foreach(string name in names)4
{5
this.comboBox1.Items.Add(name);6
}


浙公网安备 33010602011771号