/[OhmsLawCalc]/trunk/OhmsLawCalc/Form1.cs
ViewVC logotype

Contents of /trunk/OhmsLawCalc/Form1.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 26 - (show annotations) (download)
Wed Jan 29 08:13:02 2014 UTC (5 years, 7 months ago) by william
File size: 5548 byte(s)

1 //#define SHOW_DEBUG_MENU // when defined the debug menu will be shown
2 //#define ENABLE_API_TESTING // when defined will enable API_TESTING
3 using Enterprise.Logging;
4 using libOhmsLawCalc;
5 using OhmsLawCalc.UI;
6 using System;
7 using System.Collections.Generic;
8 using System.ComponentModel;
9 using System.Data;
10 using System.Drawing;
11 using System.Text;
12 using System.Windows.Forms;
13
14 namespace OhmsLawCalc
15 {
16 public partial class Form1 : Form
17 {
18 public Form1()
19 {
20 InitializeComponent();
21 NativeMethods.AttachToConsole(true);
22 gLog.CreateLog("OhmsLawCalc.log", false, LogLevel.kLogLevel_All_NoProgress, null);
23 #if SHOW_DEBUG_MENU
24 #warning SHOW_DEBUG_MENU is defined -- please undefine it after your are done testing the API
25 mnuDebug.Visible = true;
26 #else
27 mnuDebug.Visible = false;
28 #endif
29 }
30
31 private void mnuItemExit_Click(object sender, EventArgs e)
32 {
33 this.Close();
34 }
35
36 private void mnuItemTestAPI_Click(object sender, EventArgs e)
37 {
38 #if !ENABLE_API_TESTING
39 // ENABLE_API_TESTING is not defined
40 MessageBox.Show("API Testing has not been enabled.", "Feature Unavailable", MessageBoxButtons.OK, MessageBoxIcon.Warning);
41 return;
42 #else
43 // ENABLE_API_TESTING is defined
44 #warning ENABLE_API_TESTING is defined -- please undefine it after your are done testing the API
45
46 // test API
47 OhmsLawAPITest test = new OhmsLawAPITest();
48 test.TestAPI();
49 #endif
50 }
51
52 private TabPage GetCurrentTab()
53 {
54 return tbCalc.SelectedTab;
55 }
56
57 private void btnCalcCurrent_Click(object sender, EventArgs e)
58 {
59 CalcTabPage(GetCurrentTab());
60 }
61
62 private void btnCalcResistance_Click(object sender, EventArgs e)
63 {
64 CalcTabPage(GetCurrentTab());
65 }
66
67 private void btnCalcVoltage_Click(object sender, EventArgs e)
68 {
69 CalcTabPage(GetCurrentTab());
70 }
71
72 private void btnCalcPower_Click(object sender, EventArgs e)
73 {
74 CalcTabPage(GetCurrentTab());
75 }
76
77 private void btnCurrentReset_Click(object sender, EventArgs e)
78 {
79 ResetTabPage(GetCurrentTab());
80 }
81 private void btnResistanceReset_Click(object sender, EventArgs e)
82 {
83 ResetTabPage(GetCurrentTab());
84 }
85 private void btnVoltageReset_Click(object sender, EventArgs e)
86 {
87 ResetTabPage(GetCurrentTab());
88 }
89 private void btnPowerReset_Click(object sender, EventArgs e)
90 {
91 ResetTabPage(GetCurrentTab());
92 }
93
94 private void ResetTabPage(TabPage tp)
95 {
96 foreach (var c in tp.Controls)
97 {
98 var p = (c as IResetInput);
99 if (p != null)
100 {
101 p.Reset();
102 }
103 }
104 }
105
106 private void CalcTabPage(TabPage tp)
107 {
108 List<OhmsLawValue> values = new List<OhmsLawValue>();
109
110 OhmsLawValueType type = OhmsLawValueType.None;
111 if (tp.Tag != null)
112 {
113 type = (OhmsLawValueType)tp.Tag;
114 }
115 foreach (var c in tp.Controls)
116 {
117 var p = (c as ICalc);
118 if (p != null)
119 {
120 OhmsLawValueType _type; //not used
121 var v = p.GetValue(out _type);
122 if (v.Value != 0)
123 {
124 values.Add(v);
125 }
126 }
127 }
128 if (values.Count == 0) { return; }
129 var result = OhmsLaw.CreateInstance(type, values);
130
131 result = OhmsLaw.AutoConvertResult(result);
132
133 ISetValue sv = GetResultControlForTabPage(tp);
134 if (sv != null)
135 {
136 sv.SetValue(result);
137 }
138 }
139
140 private ISetValue GetResultControlForTabPage(TabPage tp)
141 {
142 OhmsLawValueType type = OhmsLawValueType.None;
143 Control ctl = new Control();
144 if (tp.Tag != null)
145 {
146 type = (OhmsLawValueType)tp.Tag;
147 }
148 foreach (var c in tp.Controls)
149 {
150 var p = (c as ICalc);
151 if (p != null)
152 {
153 OhmsLawValueType _type;
154 var v = p.GetValue(out _type);
155 if (_type == type)
156 {
157 ctl = (c as Control);
158 break;
159 }
160 }
161 }
162
163 if (ctl != null)
164 {
165 return (ctl as ISetValue);
166 }
167 return null;
168 }
169
170 private void Form1_Load(object sender, EventArgs e)
171 {
172 var tp = tbCalc.TabPages["tpCurrent"];
173 tp.Tag = OhmsLawValueType.Current;
174 tp = tbCalc.TabPages["tpResistance"];
175 tp.Tag = OhmsLawValueType.Resistance;
176 tp = tbCalc.TabPages["tpVoltage"];
177 tp.Tag = OhmsLawValueType.Voltage;
178 tp = tbCalc.TabPages["tpPower"];
179 tp.Tag = OhmsLawValueType.Power;
180 }
181 }
182 }

  ViewVC Help
Powered by ViewVC 1.1.22