如何将enum中的值作为ComboBox选项?

我们常看到VS.net中可以将一些枚举型的值放到下拉框中显示,就不禁自问:如何实现的?

 1 None.gif using  System;
 2 None.gif
 3 None.gif public   class  GetNamesTest 
 4 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 5InBlock.gif    enum Colors 
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif
 7InBlock.gif      Red, Green, Blue, Yellow 
 8ExpandedSubBlockEnd.gif    }
;
 9InBlock.gif    enum Styles 
10ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif
11InBlock.gif      Plaid, Striped, Tartan, Corduroy 
12ExpandedSubBlockEnd.gif    }
;
13InBlock.gif
14InBlock.gif    public static void Main() 
15ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
16InBlock.gif        Console.WriteLine("The values of the Colors Enum are:");
17InBlock.gif        foreach(string s in Enum.GetNames(typeof(Colors)))
18InBlock.gif            Console.WriteLine(s);
19InBlock.gif        Console.WriteLine();
20InBlock.gif        Console.WriteLine("The values of the Styles Enum are:");
21InBlock.gif        foreach(string s in Enum.GetNames(typeof(Styles)))
22InBlock.gif            Console.WriteLine(s);
23ExpandedSubBlockEnd.gif    }

24InBlock.gif


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

转载于:https://www.cnblogs.com/begincsdn/archive/2005/07/12/191666.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值