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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 54 - (show annotations) (download)
Sat Mar 14 17:23:49 2015 UTC (4 years, 10 months ago) by william
File size: 6857 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
30 private OhmsLawUnitType _LastUnitType;
31 public OhmsLawUnitType LastUnitType
32 {
33 get { return _LastUnitType; }
34 set { _LastUnitType = value; }
35 }
36
37 private void CircuitValue_Load(object sender, EventArgs e)
38 {
39 Reset();
40 bLoadDone = true;
41 }
42
43 private string CreateUnitString(OhmsLawUnitType unit)
44 {
45 if (unit == OhmsLawUnitType.Default)
46 {
47 return string.Format("{0}", CreateValueTypeString());
48 }
49 else
50 {
51 return string.Format("{0} {1}", unit.ToString(), CreateValueTypeString());
52 }
53 }
54
55 private string CreateValueTypeString()
56 {
57 switch (this.ValueType)
58 {
59 case OhmsLawValueType.Power: return "Watts";
60 case OhmsLawValueType.Current: return "Amps";
61 case OhmsLawValueType.Resistance: return "Ohms";
62 case OhmsLawValueType.Voltage: return "Volts";
63 case OhmsLawValueType.None: return "None";
64 default:
65 throw new InvalidOperationException(string.Format("Value: 0x{0:x8} is not defined in OhmsLawValueType", (uint)this.ValueType));
66 }
67 }
68 private int GetUnitTypeIndex(OhmsLawUnitType unit)
69 {
70 switch (unit)
71 {
72 case OhmsLawUnitType.Pico: return 0;
73 case OhmsLawUnitType.Nano: return 1;
74 case OhmsLawUnitType.Micro: return 2;
75 case OhmsLawUnitType.Milli: return 3;
76 case OhmsLawUnitType.Default: return 4;
77 case OhmsLawUnitType.Kilo: return 5;
78 case OhmsLawUnitType.Mega: return 6;
79 case OhmsLawUnitType.Giga: return 7;
80 default: return -1;
81 }
82
83 }
84 private OhmsLawUnitType GetUnitType()
85 {
86 int index = comboUnits.SelectedIndex;
87 switch (index)
88 {
89 case 0: return OhmsLawUnitType.Pico;
90 case 1: return OhmsLawUnitType.Nano;
91 case 2: return OhmsLawUnitType.Micro;
92 case 3: return OhmsLawUnitType.Milli;
93 case 4: return OhmsLawUnitType.Default;
94 case 5: return OhmsLawUnitType.Kilo;
95 case 6: return OhmsLawUnitType.Mega;
96 case 7: return OhmsLawUnitType.Giga;
97 }
98 return OhmsLawUnitType.None;
99 }
100
101 #region IResetInput members
102 public void Reset()
103 {
104 fBox.Value = (0).ToString();
105 comboUnits.Items.Clear();
106 comboUnits.Items.Add(CreateUnitString(OhmsLawUnitType.Pico));
107 comboUnits.Items.Add(CreateUnitString(OhmsLawUnitType.Nano));
108 comboUnits.Items.Add(CreateUnitString(OhmsLawUnitType.Micro));
109 comboUnits.Items.Add(CreateUnitString(OhmsLawUnitType.Milli));
110 comboUnits.Items.Add(CreateUnitString(OhmsLawUnitType.Default));
111 comboUnits.Items.Add(CreateUnitString(OhmsLawUnitType.Kilo));
112 comboUnits.Items.Add(CreateUnitString(OhmsLawUnitType.Mega));
113 comboUnits.Items.Add(CreateUnitString(OhmsLawUnitType.Giga));
114
115 comboUnits.Text = comboUnits.Items[DefaultValueIndex].ToString();
116 bFirstConversion = true;
117 }
118 #endregion
119
120 #region ICalc members
121 public OhmsLawValue GetValue(out OhmsLawValueType type)
122 {
123 type = this.ValueType;
124 OhmsLawValue v = OhmsLawValue.Empty;
125 float val = fBox.ToSingle();
126 v = new OhmsLawValue(this.ValueType, GetUnitType(), val);
127 return v;
128 }
129 #endregion
130
131 #region ISetValue, IGetValue members
132 public void SetValue(OhmsLawValue value)
133 {
134 OhmsLawValueType type = value.ValueType;
135 if (this.ValueType != type)
136 {
137 throw new InvalidProgramException(string.Format("Cannot set value. Requested type was: '{0}' but control type was: '{1}'", type.ToString(), this.ValueType.ToString()));
138 }
139 fBox.Value = Convert.ToSingle(value.Value).ToString();
140
141 var index = GetUnitTypeIndex(value.UnitType);
142 comboUnits.SelectedIndex = index;
143 }
144 public OhmsLawValue GetValue()
145 {
146 OhmsLawValueType _type;
147 return GetValue(out _type);
148 }
149 #endregion
150
151 bool bFirstConversion = true;
152 bool bLoadDone = false;
153 private void comboUnits_SelectedIndexChanged(object sender, EventArgs e)
154 {
155
156 if (!bLoadDone) { return; }
157 if (bFirstConversion)
158 {
159 var result = new OhmsLawValue(this.ValueType, OhmsLawUnitType.Default, fBox.ToSingle());
160 result = OhmsLaw.ConvertResult(result, GetUnitType());
161 SetValue(result);
162 bFirstConversion = false;
163 LastUnitType = GetUnitType();
164 }
165 else
166 {
167 var unit = GetUnitType();
168 var result = new OhmsLawValue(this.ValueType, LastUnitType, fBox.ToSingle());
169 result = OhmsLaw.ConvertResult(result, OhmsLawUnitType.Default);
170 result = OhmsLaw.ConvertResult(result, GetUnitType());
171 SetValue(result);
172 LastUnitType = GetUnitType();
173 }
174 // var unit = GetUnitType();
175 // var result = GetValue();
176 // result = OhmsLaw.ConvertResult(result, unit);
177 //SetValue(result);
178 }
179
180 //private void comboUnits_SelectedValueChanged(object sender, EventArgs e)
181 //{
182 // int index = comboUnits.SelectedIndex;
183 // string t = comboUnits.Items[index].ToString();
184 //}
185 }
186 }

  ViewVC Help
Powered by ViewVC 1.1.22