/[gr2lib]/trunk/gr2lib/core/ui/typeeditors/ComboValueSelector.cs
ViewVC logotype

Contents of /trunk/gr2lib/core/ui/typeeditors/ComboValueSelector.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 160 - (show annotations) (download)
Thu Jul 22 10:20:55 2010 UTC (9 years, 1 month ago) by william
File size: 2297 byte(s)
Add UIEditor to edit EnumType Values via a dropdown combobox

1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Drawing;
5 using System.Data;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using gr2lib.core.ui.rigeditor.interfaces;
10 using gr2lib.core.helpers;
11
12 namespace gr2lib.core.ui.typeeditors
13 {
14 public partial class ComboValueSelector<ENUM_TYPE, ENUM_BASE_TYPE> : ComboBox, IValueType<NameValuePair<string, ENUM_TYPE>>
15 where ENUM_BASE_TYPE : IConvertible
16 {
17 private NameValuePair<string, ENUM_TYPE> _Value;
18 public NameValuePair<string, ENUM_TYPE> Value { get { return _Value; } set { _Value = value; } }
19
20 public ComboValueSelector()
21 {
22 InitializeComponent();
23 this.Value = new NameValuePair<string, ENUM_TYPE>("", default(ENUM_TYPE));
24 this.PopulateComboBox();
25 this.SelectedIndexChanged +=new EventHandler(ComboValueSelector_SelectedIndexChanged);
26 }
27 public ComboValueSelector(object value)
28 {
29 InitializeComponent();
30 this.Value = (NameValuePair<string, ENUM_TYPE>)value;
31 this.PopulateComboBox();
32 this.SelectedIndexChanged += new EventHandler(ComboValueSelector_SelectedIndexChanged);
33 }
34
35 private void PopulateComboBox()
36 {
37 int curr_value = Convert.ToInt32(Value.Value);
38 foreach (int value in Enum.GetValues(typeof(ENUM_TYPE)))
39 {
40 string name = Enum.GetName(typeof(ENUM_TYPE), value);
41 this.Items.Add(name + " {" + value.ToString() + "}");
42 if (curr_value == value) this.SelectedIndex = value;
43 }
44 }
45
46 private void ComboValueSelector_SelectedIndexChanged(object sender, EventArgs e)
47 {
48 string[] lookup = this.Items[this.SelectedIndex].ToString().Replace(" ","").Split('{');
49 string name = lookup[0];
50 int value = Convert.ToInt32(lookup[1].Replace("}",""));
51 this.Value = new NameValuePair<string, ENUM_TYPE>(name, ValueToEnum(value));
52 }
53
54 public ENUM_TYPE ValueToEnum(int number)
55 {
56 return (ENUM_TYPE)Enum.ToObject(typeof(ENUM_TYPE), number);
57 }
58 }
59 }

  ViewVC Help
Powered by ViewVC 1.1.22