我们常看到VS.net中可以将一些枚举型的值放到下拉框中显示,就不禁自问:如何实现的?
那么加到下拉框也就很简单了。
1using System;
2
3public 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
2
3public 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
那么加到下拉框也就很简单了。
1string [] names = Enum.GetNames(typeof(DockStyle));
2comboBox1.Items.Clear();
3foreach(string name in names)
4{
5this.comboBox1.Items.Add(name);
6}
2comboBox1.Items.Clear();
3foreach(string name in names)
4{
5this.comboBox1.Items.Add(name);
6}