利用反射遍历Enum,并显示在ComboBox上,根据枚举的值,选中在comboBox中指定的项
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Threading;
9 using System.Windows.Forms;
10
11 namespace BrowserDemo
12 {
13 public partial class Form8 : Form
14 {
15 public Form8()
16 {
17 InitializeComponent();
18 }
19
20 private void button1_Click( object sender, EventArgs e)
21 {
22 DoWork();
23 }
24 private void DoWork()
25 {
26 Thread t = new Thread( new ThreadStart( this .DoSomething));
27 t.Start();
28 }
29 private void DoSomething()
30 {
31 MessageBox.Show( " thread start " );
32 }
33
34 private void button2_Click( object sender, EventArgs e)
35 {
36 foreach (var v in typeof (AA).GetFields())
37 {
38 if (v.FieldType.IsEnum == true )
39 {
40 this .comboBox1.Items.Add(v.Name);
41 }
42 }
43 this .comboBox1.SelectedIndex = 1 ;
44
45
46 }
47 public enum AA
48 {
49 B,
50 C
51 }
52
53 private void button3_Click( object sender, EventArgs e)
54 {
55
56 AA a = AA.B;
57 MessageBox.Show(a.ToString());
58 this .comboBox1.SelectedIndex = this .comboBox1.FindString(a.ToString());
59 }
60 }
61 }
62
是些小技巧,但每次要用的时候都得google一遍,真的很痛恨自己这一点,怎么记性就不能再好一点,很耽误时间。在这里记录一下吧
查看更多关于利用反射遍历Enum,并显示在ComboBox上,根据枚举的值,选中在comboBox中指定的项的详细内容...