/[OhmsLawCalc]/trunk/OhmsLawCalc/UI/CircuitValue.cs
ViewVC logotype

Contents of /trunk/OhmsLawCalc/UI/CircuitValue.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 23 - (show annotations) (download)
Wed Jan 29 08:07:43 2014 UTC (5 years, 11 months ago) by william
File size: 5160 byte(s)

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 libOhmsLawCalc;
10
11 namespace OhmsLawCalc.UI
12 {
13 public partial class CircuitValue : UserControl, IResetInput, ICalc, ISetValue, IGetValue
14 {
15 const int DefaultValueIndex = 4;
16 public CircuitValue()
17 {
18 InitializeComponent();
19 ValueType = OhmsLawValueType.None;
20 }
21
22 private OhmsLawValueType _ValueType;
23 [Browsable(true)]
24 public OhmsLawValueType ValueType
25 {
26 get { return _ValueType; }
27 set { _ValueType = value; }
28 }
29 private void CircuitValue_Load(object sender, EventArgs e)
30 {
31 Reset();
32 }
33
34 private string CreateUnitString(OhmsLawUnitType unit)
35 {
36 if (unit == OhmsLawUnitType.Default)
37 {
38 return string.Format("{0}", CreateValueTypeString());
39 }
40 else
41 {
42 return string.Format("{0} {1}", unit.ToString(), CreateValueTypeString());
43 }
44 }
45
46 private string CreateValueTypeString()
47 {
48 switch (this.ValueType)
49 {
50 case OhmsLawValueType.Power: return "Watts";
51 case OhmsLawValueType.Current: return "Amps";
52 case OhmsLawValueType.Resistance: return "Ohms";
53 case OhmsLawValueType.Voltage: return "Volts";
54 case OhmsLawValueType.None: return "None";
55 default:
56 throw new InvalidOperationException(string.Format("Value: 0x{0:x8} is not defined in OhmsLawValueType", (uint)this.ValueType));
57 }
58 }
59 private int GetUnitTypeIndex(OhmsLawUnitType unit)
60 {
61 switch (unit)
62 {
63 case OhmsLawUnitType.Pico: return 0;
64 case OhmsLawUnitType.Nano: return 1;
65 case OhmsLawUnitType.Micro: return 2;
66 case OhmsLawUnitType.Milli: return 3;
67 case OhmsLawUnitType.Default: return 4;
68 case OhmsLawUnitType.Kilo: return 5;
69 case OhmsLawUnitType.Mega: return 6;
70 case OhmsLawUnitType.Giga: return 7;
71 default: return -1;
72 }
73 }
74 private OhmsLawUnitType GetUnitType()
75 {
76 int index = comboUnits.SelectedIndex;
77 switch (index)
78 {
79 case 0: return OhmsLawUnitType.Pico;
80 case 1: return OhmsLawUnitType.Nano;
81 case 2: return OhmsLawUnitType.Micro;
82 case 3: return OhmsLawUnitType.Milli;
83 case 4: return OhmsLawUnitType.Default;
84 case 5: return OhmsLawUnitType.Kilo;
85 case 6: return OhmsLawUnitType.Mega;
86 case 7: return OhmsLawUnitType.Giga;
87 }
88 return OhmsLawUnitType.None;
89 }
90
91 #region IResetInput members
92 public void Reset()
93 {
94 fBox.Value = (0).ToString();
95 comboUnits.Items.Clear();
96 comboUnits.Items.Add(CreateUnitString(OhmsLawUnitType.Pico));
97 comboUnits.Items.Add(CreateUnitString(OhmsLawUnitType.Nano));
98 comboUnits.Items.Add(CreateUnitString(OhmsLawUnitType.Micro));
99 comboUnits.Items.Add(CreateUnitString(OhmsLawUnitType.Milli));
100 comboUnits.Items.Add(CreateUnitString(OhmsLawUnitType.Default));
101 comboUnits.Items.Add(CreateUnitString(OhmsLawUnitType.Kilo));
102 comboUnits.Items.Add(CreateUnitString(OhmsLawUnitType.Mega));
103 comboUnits.Items.Add(CreateUnitString(OhmsLawUnitType.Giga));
104
105 comboUnits.Text = comboUnits.Items[DefaultValueIndex].ToString();
106 }
107 #endregion
108
109 #region ICalc members
110 public OhmsLawValue GetValue(out OhmsLawValueType type)
111 {
112 type = this.ValueType;
113 OhmsLawValue v = OhmsLawValue.Empty;
114 float val = fBox.ToSingle();
115 v = new OhmsLawValue(this.ValueType, GetUnitType(), val);
116 return v;
117 }
118 #endregion
119
120 #region ISetValue, IGetValue members
121 public void SetValue(OhmsLawValue value)
122 {
123 OhmsLawValueType type = value.ValueType;
124 if (this.ValueType != type)
125 {
126 throw new InvalidProgramException(string.Format("Cannot set value. Requested type was: '{0}' but control type was: '{1}'", type.ToString(), this.ValueType.ToString()));
127 }
128 fBox.Value = Convert.ToSingle(value.Value).ToString();
129
130 var index = GetUnitTypeIndex(value.UnitType);
131 comboUnits.SelectedIndex = index;
132 }
133 public OhmsLawValue GetValue()
134 {
135 OhmsLawValueType _type;
136 return GetValue(out _type);
137 }
138 #endregion
139 }
140 }

  ViewVC Help
Powered by ViewVC 1.1.22