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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/OhmsLawCalc/UI/CircuitValue.cs	2014/01/29 07:31:28	21
+++ trunk/OhmsLawCalc/UI/CircuitValue.cs	2014/01/29 07:57:25	22
@@ -10,7 +10,7 @@
 
 namespace OhmsLawCalc.UI
 {
-    public partial class CircuitValue : UserControl, IResetInput, ICalc
+    public partial class CircuitValue : UserControl, IResetInput, ICalc, ISetValue, IGetValue
     {
         const int DefaultValueIndex = 4;
         public CircuitValue()
@@ -56,7 +56,21 @@
                     throw new InvalidOperationException(string.Format("Value: 0x{0:x8} is not defined in OhmsLawValueType", (uint)this.ValueType));
             }
         }
-
+        private int GetUnitTypeIndex(OhmsLawUnitType unit)
+        {
+            switch (unit)
+            {
+                case OhmsLawUnitType.Pico: return 0;
+                case OhmsLawUnitType.Nano: return 1;
+                case OhmsLawUnitType.Micro: return 2;
+                case OhmsLawUnitType.Milli: return 3;
+                case  OhmsLawUnitType.Default: return 4;
+                case OhmsLawUnitType.Kilo: return 5;
+                case OhmsLawUnitType.Mega: return 6;
+                case OhmsLawUnitType.Giga: return 7;
+                default: return -1;
+            }
+        }
         private OhmsLawUnitType GetUnitType()
         {
             int index = comboUnits.SelectedIndex;
@@ -93,13 +107,35 @@
         #endregion
 
         #region ICalc members
-        public OhmsLawValue GetValue()
+        public OhmsLawValue GetValue(out OhmsLawValueType type)
         {
+            type = this.ValueType;
             OhmsLawValue v = OhmsLawValue.Empty;
             float val = fBox.ToSingle();
             v = new OhmsLawValue(this.ValueType, GetUnitType(), val);
             return v;
         }
         #endregion
+
+        #region ISetValue, IGetValue members
+        public void SetValue(OhmsLawValue value)
+        {
+            OhmsLawValueType type = value.ValueType;
+            if (this.ValueType != type)
+            {
+                throw new InvalidProgramException(string.Format("Cannot set value. Requested type was: '{0}' but control type was: '{1}'", type.ToString(), this.ValueType.ToString()));
+            }
+
+            fBox.Value = Convert.ToSingle(value.Value).ToString();
+
+            var index = GetUnitTypeIndex(value.UnitType);
+            comboUnits.SelectedIndex = index;
+        }
+        public OhmsLawValue GetValue()
+        {
+            OhmsLawValueType _type;
+            return GetValue(out _type);
+        }
+        #endregion
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22