0
|
1 using System;
|
|
2 using System.Collections;
|
|
3 using System.ComponentModel;
|
|
4 using System.Windows.Forms;
|
|
5 using System.Drawing.Design;
|
|
6
|
|
7 using BLToolkit.ComponentModel;
|
|
8 using System.Reflection;
|
|
9
|
|
10 namespace BLToolkit.Demo.Controls
|
|
11 {
|
|
12 public partial class EnumSelector : GroupBox
|
|
13 {
|
|
14 public EnumSelector()
|
|
15 {
|
|
16 InitializeComponent();
|
|
17 }
|
|
18
|
|
19 public EnumSelector(IContainer container)
|
|
20 {
|
|
21 container.Add(this);
|
|
22
|
|
23 InitializeComponent();
|
|
24 }
|
|
25
|
|
26 [RefreshProperties(RefreshProperties.Repaint)]
|
|
27 [Category("Data")]
|
|
28 [Bindable(true)]
|
|
29 public object Value
|
|
30 {
|
|
31 get
|
|
32 {
|
|
33 foreach (RadioButton r in _controls.Keys)
|
|
34 if (r.Checked)
|
|
35 return Enum.Parse(_valueType, (string)_controls[r]);
|
|
36
|
|
37
|
|
38 return -1;
|
|
39 }
|
|
40 set
|
|
41 {
|
|
42 if (value != null)
|
|
43 {
|
|
44 string s = value.ToString();
|
|
45
|
|
46 foreach (DictionaryEntry de in _controls)
|
|
47 {
|
|
48 RadioButton r = (RadioButton)de.Key;
|
|
49
|
|
50 if (r.Checked != (de.Value.ToString() == s))
|
|
51 r.Checked = !r.Checked;
|
|
52 }
|
|
53 }
|
|
54 }
|
|
55 }
|
|
56
|
|
57 const FieldAttributes EnumField = FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal;
|
|
58
|
|
59 private Hashtable _controls = new Hashtable();
|
|
60
|
|
61 private Type _valueType;
|
|
62 [RefreshProperties(RefreshProperties.Repaint)]
|
|
63 [DefaultValue(null)]
|
|
64 [Category("Data")]
|
|
65 [TypeConverter(typeof(TypeTypeConverter))]
|
|
66 [Editor(typeof(EnumEditor), typeof(UITypeEditor))]
|
|
67 public Type ValueType
|
|
68 {
|
|
69 get { return _valueType; }
|
|
70 set
|
|
71 {
|
|
72 _valueType = value;
|
|
73
|
|
74 foreach (Control c in _controls.Keys)
|
|
75 Controls.Remove(c);
|
|
76
|
|
77 FieldInfo[] fields = _valueType.GetFields();
|
|
78
|
|
79 foreach (FieldInfo fi in fields)
|
|
80 {
|
|
81 if ((fi.Attributes & EnumField) == EnumField)
|
|
82 {
|
|
83 RadioButton r = new RadioButton();
|
|
84
|
|
85 Controls.Add(r);
|
|
86 _controls.Add(r, fi.Name);
|
|
87
|
|
88 r.Text = fi.Name;
|
|
89 r.Name = fi.Name + "RadioButton";
|
|
90 r.TabIndex = _controls.Count;
|
|
91 r.AutoSize = true;
|
|
92 r.Location = new System.Drawing.Point(10, 23 * _controls.Count - 4);
|
|
93 r.UseVisualStyleBackColor = true;
|
|
94 }
|
|
95 }
|
|
96 }
|
|
97 }
|
|
98
|
|
99 class EnumEditor : ComponentModel.Design.TypeEditor
|
|
100 {
|
|
101 protected override bool FilterTypeList(Type type)
|
|
102 {
|
|
103 return type.IsPublic && type.IsEnum;
|
|
104 }
|
|
105 }
|
|
106 }
|
|
107 }
|